简体   繁体   English

如何从左侧外部联接中查找具有AND条件的完全匹配项

[英]How to find exact match item with AND condition from left outer join

I have a MySQL table 我有一个MySQL表
Booktable Booktable

+--------+-------------+-----+  
| bookno | bookname    | ... |  
+--------+-------------+-----+  
| 1      | FINALFANTASY| ... |  
+--------+-------------+-----+  

Authortable Authortable

+--------+-------------+-----+  
| bookno | Authorname  | ... |  
+--------+-------------+-----+  
| 1      | SQUARE      | ... |  
+--------+-------------+-----+  
| 1      | ENIX        | ... |  
+--------+-------------+-----+  

so I would like to make a search condition to get the book that match with the result. 因此,我想进行搜索以获取与结果相符的书。

I try with 我尝试

select b.bookname,a.authorname from booktable as b 
left outer join authortable a on b.bookno = a.bookno
where a.authorname = "square" and a.authorname = "enix"

It only work with only one where condition.but when I try with two authorname there is no result found. 它仅适用于一个where condition。但是当我尝试使用两个authorname时,没有找到结果。 what should I do ? 我该怎么办 ?

(this query it working with "OR" but not "AND" but I really want the value that match the search condition or if there are some search condition that not match but not blank it should not be showing(so or it not working in this case) (此查询使用“ OR”但不使用“ AND”,但我确实希望该值与搜索条件匹配,或者如果某些搜索条件不匹配但不为空,则不应显示该值(因此它不能在这个案例)

Use aggregation to identify which books have both the authors you want: 使用汇总来确定哪些书籍同时具有您想要的作者:

SELECT t1.bookname,
       t2.authorname
FROM booktable t1
INNER JOIN authortable t2
    ON t1.bookno = t2.bookno
INNER JOIN
(
    SELECT bookno
    FROM authortable
    WHERE authorname IN ('square', 'enix')
    GROUP BY bookno
    HAVING COUNT(DISTINCT authorname) = 2
) t3
    ON t1.bookno = t3.bookno

Demo here: 演示在这里:

SQLFiddle SQLFiddle

Tim Biegeleisen's answer is great, but in case you need exactly match, the the last SQL in the following is correct: Tim Biegeleisen的答案很好,但是如果您需要完全匹配,则下面的最后一个SQL是正确的:

SELECT * FROM book;
SELECT * FROM author;

/* this SQL will return book's author name more than 2 also true */
SELECT b.bookname, a.authorname
FROM book AS b
JOIN author AS a ON b.bookno = a.bookno
JOIN (
  SELECT bookno FROM author
  WHERE authorname in ('SQUARE', 'ENIX')
  GROUP BY 1
  HAVING count(*) = 2
) AS a2 ON b.bookno = a2.bookno;

/* this sQL will return only 2 and all matched authors: */
SELECT b.bookname, a.authorname
FROM book AS b
JOIN author AS a ON b.bookno = a.bookno
JOIN (
  SELECT bookno FROM author
  WHERE authorname in ('SQUARE', 'ENIX')
  GROUP BY 1
  HAVING count(*) = 2
) AS a2 ON b.bookno = a2.bookno
JOIN (
  SELECT bookno FROM author 
  GROUP BY 1 
  HAVING count(distinct authorname) = 2
) AS a3 ON b.bookno = a3.bookno

PS1 - no need left join PS1-无需左加入

PS2 - no need count distinct - unless your author table not design properly PS2-无需计数-除非您的作者表设计不正确

在此处输入图片说明

If title is FANTASY genre is Adventure,fantasy, and search condition is 如果标题为FANTASY,则流派为Adventure,fantasy,搜索条件为

[ADVENTURE] = found [冒险] =找到

[FANTASY] = found [幻想] =找到

[ADVENTURE,FANTASY] = found [冒险,幻想] =找到

[ADVENTURE,FANTASY,ACTION] = not found [冒险,幻想,动作] =找不到

Then the SQL will be: 然后,SQL将是:

SELECT b.bookname, a.authorname
FROM book AS b
JOIN author AS a ON b.bookno = a.bookno
JOIN author AS a1 ON b.bookno = a1.bookno AND a1.authorname = 'SQUARE'
JOIN author AS a2 ON b.bookno = a2.bookno AND a2.authorname = 'ENIX'

Above is working, and I m wondering if there is a performance improvement 以上工作正常,我想知道是否可以提高性能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM