MySQL在干草堆中搜索密钥

时间:2015-10-13 09:09:28

标签: mysql sql

我的MySQL查询有问题。

我有一个书籍数据库,其中有一个名为“作者”的字段;此字段包含本书作者的ID。例如。 16,42,100。

在作者单页中,我想拥有作者的所有书籍。

那么......什么是正确的查询?

code=input("Please enter your code ")
while len((code)) !=11: #here should be something to nullify strings inout :)
    code = input("Please enter your code and in numbers only ")

我尝试使用LIKE'$ author%',但这个解决方案有一个错误:实际上,如果$ author = 116,查询还会返回带有author = 1116的书。

提前致谢!

Mauro

2 个答案:

答案 0 :(得分:1)

你需要:

SELECT * FROM `books` WHERE `author_id` = '2'; -- Single author.

-- Multiple authors.
SELECT * FROM `books` WHERE `author_id` IN (
  SELECT `author_id` FROM (whatever)
)

答案 1 :(得分:0)

你需要find_in_set:

SELECT * FROM xxxx WHERE find_in_set(author_id, authors) > 0

你真的应该规范化那个数据库。