Ik heb de volgende database layout:
en probeer de volgende query:
met als resultaat:
1
2
3
waarbij een betere query uiteraard moet zijn:
met alleen 2 als het goede resultaat maar het gaat me om de theorie en hoe ik die fout interpreteer want de mysql documentatie zegt:
The conditional_expr used with ON is any conditional expression of the form that can be used in a WHERE clause. Generally, you should use the ON clause for conditions that specify how to join tables, and the WHERE clause to restrict which rows you want in the result set.
wat ik dus interpreteer als: het is 'beter' maar niet 'noodzakelijk' om wat voor conditionele expressie dan ook perse in de WHERE clause te zetten. Naar mijn idee zou beide queries dus hetzelfde resultaat moeten geven maar dat doet het dus niet.
waar denk ik fout?
overigens ipv een LEFT JOIN alleen een JOIN gebruiken geeft ook het correcte resultaat...
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| CREATE TABLE t1 ( id int(2) unsigned default '0' not null, name char(50) default '' not null ); INSERT INTO t1 (id, name) VALUES ('1','name A'); INSERT INTO t1 (id, name) VALUES ('2','name B'); INSERT INTO t1 (id, name) VALUES ('3','name C'); CREATE TABLE t2 ( t1_id int(2) unsigned default '0' not null, name_type char(50) default '' not null ); INSERT INTO t2 (t1_id, name_type) VALUES ('1','check'); INSERT INTO t2 (t1_id, name_type) VALUES ('2','uncheck'); INSERT INTO t2 (t1_id, name_type) VALUES ('2','check'); |
en probeer de volgende query:
code:
1
| SELECT t1.id FROM t1 LEFT JOIN t2 ON t2.t1_id = t1.id AND t2.name_type = 'uncheck' |
met als resultaat:
1
2
3
waarbij een betere query uiteraard moet zijn:
code:
1
| SELECT t1.id FROM t1 LEFT JOIN t2 ON t2.t1_id = t1.id WHERE t2.name_type = 'uncheck' |
met alleen 2 als het goede resultaat maar het gaat me om de theorie en hoe ik die fout interpreteer want de mysql documentatie zegt:
The conditional_expr used with ON is any conditional expression of the form that can be used in a WHERE clause. Generally, you should use the ON clause for conditions that specify how to join tables, and the WHERE clause to restrict which rows you want in the result set.
wat ik dus interpreteer als: het is 'beter' maar niet 'noodzakelijk' om wat voor conditionele expressie dan ook perse in de WHERE clause te zetten. Naar mijn idee zou beide queries dus hetzelfde resultaat moeten geven maar dat doet het dus niet.
waar denk ik fout?
overigens ipv een LEFT JOIN alleen een JOIN gebruiken geeft ook het correcte resultaat...