snowdog Posted January 23, 2009 Share Posted January 23, 2009 Here is the sql SELECT DISTINCT `m`.`ql_id`,`r`.`email` FROM `ql_master`.`ql_main` AS `m`, `cpcoaching`.`reminder_emails` AS `r`, `ql_master`.`ql_personal_data` AS `p`, `ql_master`.`[color=red]ql_reo[/color]` AS `reo` WHERE `r`.`client_id` = `m`.`ql_id` AND `r`.`client_id` = `p`.`ql_id` AND `reo`.`ql_id` = `m`.`ql_id` AND `reo`.`reo` = '1' AND `m`.`site_access` = '1' ORDER BY `r`.`client_id` ASC the ql_reo in red has everybody that is in the reo program listed in the datatbase, if they work multiple states then they have more than one entry, associated with their ql_id. What I need is to pull the DISTINCT list of ql_id from the ql_reo database with the corresponding information from the other databases in the join. The join works but gives me say 200 results with multiple entries for the same ql_id. When I do a manual count it should be 88. The distinct in my code does not work the way I want it to. Any ideas? I am at a loss here. New to the whole joining thing. I can do it without the joins Snowdog Quote Link to comment Share on other sites More sharing options...
btherl Posted January 24, 2009 Share Posted January 24, 2009 Let me repost that before I look at it: SELECT DISTINCT `m`.`ql_id`,`r`.`email` FROM `ql_master`.`ql_main` AS `m`, `cpcoaching`.`reminder_emails` AS `r`, `ql_master`.`ql_personal_data` AS `p`, `ql_master`.`[color=red]ql_reo[/color]` AS `reo` WHERE `r`.`client_id` = `m`.`ql_id` AND `r`.`client_id` = `p`.`ql_id` AND `reo`.`ql_id` = `m`.`ql_id` AND `reo`.`reo` = '1' AND `m`.`site_access` = '1' ORDER BY `r`.`client_id` ASC Quote Link to comment Share on other sites More sharing options...
btherl Posted January 24, 2009 Share Posted January 24, 2009 As I understand, you should only get multiple ql_id there if you also have multiple emails for each ql_id. Is that the case? You can also use GROUP BY as an alternative for DISTINCT. Quote Link to comment Share on other sites More sharing options...
fenway Posted January 27, 2009 Share Posted January 27, 2009 You can also use GROUP BY as an alternative for DISTINCT. It is so very much NOT an alternative -- they mean completely different things unless there's only a single field. Quote Link to comment Share on other sites More sharing options...
btherl Posted January 28, 2009 Share Posted January 28, 2009 What I mean is this (taken from mysql manual) In most cases, a DISTINCT clause can be considered as a special case of GROUP BY. For example, the following two queries are equivalent: SELECT DISTINCT c1, c2, c3 FROM t1 WHERE c1 > const; SELECT c1, c2, c3 FROM t1 WHERE c1 > const GROUP BY c1, c2, c3; Quote Link to comment Share on other sites More sharing options...
fenway Posted January 28, 2009 Share Posted January 28, 2009 That's true... as long as all of the column in the group by appear in distinct, they're the same.. .but that's really what distinct is meant for. Also, performance can be very different -- DISTINCT is very dumb. Quote Link to comment Share on other sites More sharing options...
btherl Posted February 1, 2009 Share Posted February 1, 2009 Is it generally better to use GROUP BY in mysql unless you really want to use DISTINCT? That's the rule for postgres. Because DISTINCT always sorts the rows, but GROUP BY can use a hash table (which is not part of the SQL spec, it's just the implementation). Quote Link to comment Share on other sites More sharing options...
fenway Posted February 2, 2009 Share Posted February 2, 2009 You almost never want DISTINCT... it's just lazy, and 99.999% of the time it's completely wrong, and returned seemingly correct results by accident. I also ever use it in "UNION DISTINCT", where it actually makes sense. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.