假设我创建了3个表格,如下所示......
create table `users` (`username` varchar(20), `other_non_relevant_field` varchar(20) );
insert into `users` values
("Bob","blah blah blah"),
("Steve","blah blah blah"),
("Sue","blah blah blah"),
("Adam","blah blah blah");
create table `table_1` (`username` varchar(20), `field_abc` varchar(20), `field_def` varchar(20), `other_non_relevant_field` varchar(20) );
insert into `table_1` values
("Steve","blue","brown","blah blah blah"),
("Sue","yellow","brown","blah blah blah"),
("Sue","pink","brown","blah blah blah"),
("Adam","green","brown","blah blah blah");
create table `table_2` (`username` varchar(20), `field_ghi` varchar(20), `field_jkl` varchar(20), `other_non_relevant_field` varchar(20) );
insert into `table_2` values
("Bob","spoon","fork","blah blah blah"),
("Bob","knife","spork","blah blah blah"),
("Steve","spoon","knife","blah blah blah"),
("Sue","spoon","fork","blah blah blah"),
("Sue","knife","spork","blah blah blah"),
("Adam","fork","knife","blah blah blah");
然后我运行此查询...
SELECT users.username,
table_1.field_abc, table_1.field_def,
table_2.field_ghi, table_2.field_jkl
FROM users
JOIN table_1 ON table_1.username = users.username
JOIN table_2 ON table_2.username = users.username
WHERE
table_1.field_abc REGEXP "(spork|pink)" OR
table_1.field_def REGEXP "(spork|pink)" OR
table_2.field_ghi REGEXP "(spork|pink)" OR
table_2.field_jkl REGEXP "(spork|pink)"
ORDER BY
(
( CASE WHEN table_1.field_abc LIKE "%spork%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_1.field_abc LIKE "%pink%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_1.field_def LIKE "%spork%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_1.field_def LIKE "%pink%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_2.field_ghi LIKE "%spork%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_2.field_ghi LIKE "%pink%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_2.field_jkl LIKE "%spork%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_2.field_jkl LIKE "%pink%" THEN 1 ELSE 0 END )
)DESC;
为什么它不会返回Bob的“spork”条目? 您可以在http://sqlfiddle.com/#!2/cbbda/5
中查看此查询不言而喻,这不是我的实际代码,但它足够相似,并且具有相同的问题,我将能够找出问题所在。
答案 0 :(得分:3)
由于默认JOIN
为INNER
,因此查询不会返回表返回NULL
的结果。使用LEFT JOIN
会返回“Bob”行,包含NULL
个值。
http://sqlfiddle.com/#!2/cbbda/7/0
以下是各种JOIN类型的直观表示: http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
SELECT users.username,
table_1.field_abc, table_1.field_def,
table_2.field_ghi, table_2.field_jkl
FROM users
LEFT JOIN table_1 ON table_1.username = users.username
LEFT JOIN table_2 ON table_2.username = users.username
WHERE
table_1.field_abc REGEXP "(spork|pink)" OR
table_1.field_def REGEXP "(spork|pink)" OR
table_2.field_ghi REGEXP "(spork|pink)" OR
table_2.field_jkl REGEXP "(spork|pink)"
ORDER BY
(
( CASE WHEN table_1.field_abc LIKE "%spork%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_1.field_abc LIKE "%pink%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_1.field_def LIKE "%spork%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_1.field_def LIKE "%pink%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_2.field_ghi LIKE "%spork%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_2.field_ghi LIKE "%pink%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_2.field_jkl LIKE "%spork%" THEN 1 ELSE 0 END ) +
( CASE WHEN table_2.field_jkl LIKE "%pink%" THEN 1 ELSE 0 END )
)DESC;
答案 1 :(得分:2)
因为在table_1元素中不存在Bob,并且JOIN丢弃所有连接表中没有匹配行的行。
为了实现您想要的效果,请在两个外观中通过LEFT JOIN更改JOIN。这样,您将使用table_1和table_2元素连接的所有“Users”元素匹配,如果未找到匹配项,则为null。