Welcome Guest!
Please login
If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:
- Receive Fast Advice
- Learn Programming Languages
- Get Professional Website Reviews
- Quick Troubleshooting Assistance
|
|
Merging These 2 Simple Queries
This is a discussion on Merging These 2 Simple Queries, within the MySQL section. This forum and the thread "Merging These 2 Simple Queries" are both part of the Programming Your Website category.
![]() ![]() |
Sep 3 2008, 04:50 AM
Post
#1
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
Say one guy wanted to merge 2 sql queries.
SELECT id FROM table1 WHERE rule='1' and SELECT id FROM table2 WHERE rule='1' How would that guy do that? Ok its me! I am stupid! arghhhhhplease help |
|
|
Sep 3 2008, 08:00 AM
Post
#2
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
It depends really...
Does each bit of data have any relation to the next and do you want everything? You can use a join... To get everything CODE SELECT t1.id, t2.if FROM table1 AS t1, table2 AS t2 WHERE t1.rule=1 OR t2.rule=1 That will return all the matches and id's as combinations of each other. If you are using foreign key references then you can use a LEFT JOIN CODE SELECT t1.id, t2.id FROM table1 AS t1 LEFT JOIN table2 AS t2 ON t1.related_column=t2.related_column WHERE t1.rule=1 AND t2.rule=1 That will only return one result. It really depends on how the tables are related. Take a look at these references to see some examples: http://dev.mysql.com/doc/refman/5.1/en/lef...timization.html http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php -------------------- |
|
|
Sep 3 2008, 02:37 PM
Post
#3
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
Thanks for the help on this.
Basically I have 2 tables that store id's for the time being while we're migrating from one table to another while keeping it backward and forward compatible. I'm hoping for this to work CODE SELECT table1.ID, table2.ID FROM table1 as t1, table2 as t2 WHERE t1.anotherID=154 OR t2.anotherID=154 and basically retrieves a singular sequence of ID's. When I throw this kind of SQL into phpmyadmin I end up with 2 columns of ID's like so...
2columns.png ( 38.03K )
Number of downloads: 5GROUP BY doesn't seem to be liking the way I'm using it ... says I'm being ambiguous |
|
|
Sep 3 2008, 06:38 PM
Post
#4
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
Group by is saying you're ambiguous because it
MySQL wants you to specify which table you are grouping by t1.id or t2.id for example. If you are pulling things from two tables that are unrelated, you are probably better off going for two queries though. -------------------- |
|
|
Sep 3 2008, 07:25 PM
Post
#5
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
I 'think' the tables are related but I could be misunderstanding the meaning.
In an ideal world my code would look like this CODE $query = 'SELECT table1.ID, table2.ID FROM table1 as t1, table2 as t2 WHERE this="1"'; $result = mysql_query($q); while($row = mysql_fetch_array($result)) { // based on the string of ID's pulled from the joint query, retrieve additional data $dataquery = 'SELECT * FROM events WHERE ID="' . $row['ID'] . '" '; // echo out information }; but with my current primitive skills to do this I'm doing something more like CODE // results from first table $query = 'SELECT ID FROM table1 WHERE this="1" GROUP BY ID'; $result = mysql_query($q); while($row = mysql_fetch_array($result)) { // based on the string of ID's pulled from the joint query, retrieve additional data $dataquery = 'SELECT * FROM events WHERE ID="' . $row['ID'] . '" '; // echo out information based on the first tables results // store this ID in an array for cross checking to avoid duplication $array_id[] = $row['ID']; }; // results for second table $query = 'SELECT ID FROM table2 WHERE this="1 GROUP BY ID"'; $result = mysql_query($q); while($row = mysql_fetch_array($result)) { // if the ID was stored in an array previously, don't do the following... if(array_search($array_id, $row['ID']) == FALSE) { // based on the string of ID's pulled from the joint query, retrieve additional data $dataquery = 'SELECT * FROM events WHERE ID="' . $row['ID'] . '" '; // echo out information based on the first tables results } }; Its basically just this one ID field that is the same between 2 tables but then from that array of ID's I then need to be able to search another table. Currently doing the duplicate method is how I'm doing it but there become a problem of duplication and replication of not only code, but of what comes out. By having it across 2 queries, I have to have additional checking in place such as "oh, if this ID was pulled out from table1, then don't display in table2 results. Then comes the issue of sorting. Its a bit of a tangle simply because its a bit of a migration to a more simple table structure / indexing thing. |
|
|
Sep 3 2008, 07:46 PM
Post
#6
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
Okay, I think I can bring the events table into this so you should only have to run one query to get everything
CODE SELECT DISTINCT e. * FROM table1 AS t1
LEFT JOIN table2 AS t2 ON ( t1.ID != t2.ID ) LEFT JOIN events AS e ON ( e.ID = t1.ID OR e.ID = t2.ID ) WHERE t1.this='1' OR t2.this='1' -------------------- |
|
|
Sep 3 2008, 10:28 PM
Post
#7
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
You'll probably want to slap me side ways... but one of those tables (table1 or table2) is 'events'.
So I used to store a connection to another table under typeID in the events table. Now we've made a joining table 'connectedEvents' so the one eventID can be connected to multiple typeID's. I figured I would reduce your query to this (taking out the distinct and the 2nd left join line) CODE SELECT * FROM events AS t1 LEFT JOIN connectedEvents AS t2 ON ( t1.eventID != t2.eventID ) WHERE t1.typeID='1' OR t2.typeID='1' Which does produce a result however its producing results with typeID of 0 and others.
Picture_2.png ( 50.61K )
Number of downloads: 5 |
|
|
Sep 4 2008, 02:01 AM
Post
#8
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
I think I may have solved it...
CODE SELECT * FROM events AS t1 LEFT JOIN connectedEvents AS t2 ON ( t1.eventID = t2.eventID ) WHERE t1.typeID='1' OR t2.typeID='1' Changing that '!=' to a '='... |
|
|
Sep 4 2008, 03:05 AM
Post
#9
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 182 Joined: 19-February 08 From: Netherlands |
If you've solved it, great. If you haven't yet, maybe it would be a good idea to also post both table structures and explain it.
Then it would be easier for us to know what kind of relation the tables have. -------------------- fresh-style.nl - small webdesign & development projects |
|
|
Oct 1 2008, 10:47 PM
Post
#10
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 146 Joined: 3-July 08 |
If you've solved it, great. If you haven't yet, maybe it would be a good idea to also post both table structures and explain it. Then it would be easier for us to know what kind of relation the tables have. Thanks heaps. It took me a while but based on rakuli's example I was able to get it going. I am still having some challenges though, such as how to do a simple thing like getting an array of ID's from one table, and then doing a count of the times that ID occurs in another table within the same query. CODE SELECT COUNT(events.eventID) FROM events, locations WHERE events.locID = locations.locID AND locations.region_id='" . $row_region['region_id'] . "' AND (DATE(CONCAT(events.year ,'-', events.month, '-', events.day, ' ', events.reghour, ':', events.regminute, ':00')) > DATE_ADD(NOW(), INTERVAL -1 day) ) I have made a simple diagram of the table structure if that helps at all! Thanks for your time.
tablequery1.jpg ( 55.63K )
Number of downloads: 5editors note: I should add that simply I am attempting to display a list of the regions and the number of events coming up for those. This post has been edited by cosmicbdog: Oct 1 2008, 10:51 PM |
|
|
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
1 | Daniela | 487 | 15th February 2008 - 06:58 AM Last post by: craig |
|||
![]() |
14 | edd | 609 | 15th May 2008 - 01:59 AM Last post by: Antti |
|||
![]() |
13 | shanedizzle | 457 | 11th April 2008 - 03:31 PM Last post by: joey |
|||
![]() |
5 | paintingtheweb | 341 | 14th April 2008 - 06:42 PM Last post by: paintingtheweb |
|||
![]() |
0 | mv08jml | 288 | 19th June 2008 - 12:12 PM Last post by: mv08jml |
|||






Sep 3 2008, 04:50 AM













