[RESOLVED] UNION & GROUP BY (simple)
Hi, i have a problem with using union and group by together.
the following:
PHP Code:
SELECT * FROM ` table1 ` UNION SELECT * FROM ` table2 ` WHERE metal = '1' GROUP BY width
this returns in my dropdown:
the group by seems to work but only for one table at a time.
i would like it to work with both tables so it should only display 2,4, 5,6.
Any Ideas why its not working correctly?
Regards
David
Does this work?
Code:
SELECT * FROM `table1` WHERE metal = '1' GROUP BY width UNION SELECT * FROM `table2` WHERE metal = '1' GROUP BY width
Sorry, Sadly it still shows duplicate numbers still.
Its like the code before and after UNION are handled like 2 seperate querys.
Im pretty stuck with this.
I think that's how a UNION works (as far as I know) it just executes multiple statements and then pieces them together. Generally UNION by default is supposed to return distinct results, but maybe try UNION DISTINCT?
PHP Code:
select * FROM ( SELECT * FROM ` table1 ` UNION SELECT * FROM ` table2 ` WHERE metal = '1' ) GROUP BY width
Thanks Alan it had an error as it needs to be defined an alias, but you helped me with the overall design of the statement. Thanks Allot !!
PHP Code:
select * FROM ( SELECT * FROM ` table1 ` WHERE metal = '1' UNION SELECT * FROM ` table2 ` WHERE metal = '1' )AS randomname GROUP BY width
Whoops, sorry about that! Glad you got it sorted!
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks