查找可用性为= 0或未知

时间:2016-06-27 18:09:36

标签: sql join anti-join

我有一张表Books,其中有许多properties。属性存储为键和值。

所以,如果书籍是:

1  LOTR
2  Harry Potter 1
3  Harry Potter 2

属性

id  book_id  key        value
1   1        available  0
2   2        available  10
3   2        author     Rowling
4   3        author     Rowling

我想把结果作为:

1  LOTR
3  Harry Potter 2

因为Book id 1有0可用性,2有10,而3没有任何可用性信息。

我知道我可以使用反连接,但我不知道如何使用它。我对反连接有点新意。

感谢任何帮助。

3 个答案:

答案 0 :(得分:3)

我并非100%确定我理解您的问题,但假设您想要退回; We need a file in %temp% that the external command can write to. !tempfile TMPINC ; This !system command tries to find the file in the Windows directory !system `cd /D %windir% & FOR /F %A IN ('dir /A:-D /T:W /O:D /B') DO @(> "${TMPINC}" echo.!define RECENTMOD "%~fA")` !include "${TMPINC}" !delfile "${TMPINC}" !undef TMPINC !ifndef RECENTMOD !error "No files found!" !endif Section File "${RECENTMOD}" SectionEnd 表中没有可用性的所有图书,那么这里有一个选项使用properties

outer join

根据您的数据库,您可能需要select b.* from books b left join properties p on b.id = p.book_id and p.key = 'available' and p.value > 0 where p.id is null cast中的value列。

答案 1 :(得分:0)

试试这个:

SELECT b.book_id, a.key, a.value
FROM Books AS B INNER JOIN AnotherTable AS A B.book_id = a.book_id
WHERE a.key = 'available' and (a.value = 0 OR a.value is null)

答案 2 :(得分:0)

SELECT book_id, title
FROM Books as B
WHERE B.book_id NOT IN (
                SELECT P.book_id 
                FROM properties as P
                WHERE P.key = available AND P.value <> 0)

请注意,<>表示NOT EQUAL

相关问题