Py学习  »  DATABASE

子查询上的类MySQL

DonP • 5 年前 • 1577 次点击  

该表只有六个条目包含作者姓名,但在某些情况下,该领域有多个作者,我需要获得他们各自的传记。现在这没有错误,但它只拉一个包含两个作者姓名的条目,而不是包含实际传记的两个条目。我只需要两个单一的名字条目,而不是包含两者的条目,但它给出了相反的结果。

AuthorName有这样的东西:

Joe Blow
Jane Doe
Jow Blow and Jane Doe

下面是问题

SELECT a.`ID` AS AuthorID, `AuthorName`, `AuthorPhoto`, `AuthorBio`, `Email`,
FROM authorbiographies a 
WHERE `AuthorName` LIKE CONCAT('%',(
SELECT `AuthorName` )
FROM authorbiographies WHERE `AuthorName` LIKE '% and %),'%'
) 
AND `ID` <> 3
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54769
 
1577 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Gordon Linoff
Reply   •   1 楼
Gordon Linoff    5 年前

SELECT ab.*
FROM authorbiographies ab
WHERE ab.authorname NOT LIKE '% and %';

你似乎只想挑出那些没有 ' and ' 在他们身上。

Nick
Reply   •   2 楼
Nick    5 年前

这可以通过连接所有 authorname and 所有的派生表 作者姓名 作者姓名

SELECT a1.id, a1.authorname, a1.authorbio
FROM (SELECT *
      FROM authorbiographies
      WHERE authorname NOT LIKE '% and %'
      ) a1
JOIN (SELECT *
      FROM authorbiographies
      WHERE authorname like '% and %'
      ) a2 on a2.authorname LIKE concat('%', a1.authorname, '%')

输出(用于您的示例)

id  authorname  authorbio
1   Joe Blow    has lived a long time
2   Jane Doe    wrote several books

Demo on SQLFiddle