MySQL:列出数据及其来自其他表的相关数据(如果可用)

标签 mysql sql database select join

这是我的场景:

image_book_rel包含两个表之间的关系,即book_tableimage_table

我需要从表 book_table 中获取书籍列表,并从表 image_table 中获取相关图像数据如果可用

表格大致如下(简化):

/* book_table */
id       title      price
-------- ---------- ------
1        Book 1     $10
2        Book 2     $13
3        Book 3     $15
4        Book 4     $20
5        Book 5     $12
6        Book 6     $10
7        Book 7     $14

/* image_table */
id       description      file
-------- ---------------- -------------
20       Image of Book 2  book_img2.jpg
30       Image of Book 3  book_img3.jpg
50                        book_img5.jpg
70       Image of Book 7  book_img7.jpg

/* image_book_rel */
id       book_id   image_id
-------- --------- ---------
1        2         20
2        3         30
3        5         50
4        7         70

下面是我需要的查询结果:

/* Expected result: */
book_id  image_id  filename        book_title  img_desc
-------- --------- --------------- ----------- -----------------
1                                  Book 1
2        20        book_img2.jpg   Book 2       Image of Book 2
3        30        book_img3.jpg   Book 3       Image of Book 3
4                                  Book 4
5        50        book_img5.jpg   Book 5
6                                  Book 6
7        70        book_img7.jpg   Book 7       Image of Book 7

这是我尝试过的:

SELECT bk.id AS book_id, im.id AS image_id, im.file AS filename, bk.title AS book_title, im.description AS img_desc
    FROM book_table AS bk
        LEFT JOIN image_book_rel AS bi
            ON bk.id = bi.book_id
        LEFT JOIN image_table AS im
            ON bi.image_id = im.id
    WHERE bk.id = bk.id
        AND bi.book_id = bk.id
        AND bi.image_id = im.id
    ORDER BY bk.id

上述查询的结果 有图像的书籍。我需要检索所有 书籍。我该怎么做?

最佳答案

尝试不使用 WHERE 并使用 IFNULL 将空值替换为空字符串:

SELECT IFNULL(bk.id,'') AS book_id, IFNULL(im.id,'') AS image_id, IFNULL(im.file,'') AS filename, IFNULL(bk.title,'') AS book_title, IFNULL(im.description,'') AS img_desc
    FROM book_table AS bk
        LEFT JOIN image_book_rel AS bi
            ON bk.id = bi.book_id
        LEFT JOIN image_table AS im
            ON bi.image_id = im.id
    ORDER BY bk.id

结果:

BOOK_ID   IMAGE_ID  FILENAME        BOOK_TITLE   IMG_DESC
1                                   Book 1       
2         20        book_img2.jpg   Book 2       Image of Book 2
3         30        book_img3.jpg   Book 3       Image of Book 3
4                                   Book 4       
5         50        book_img5.jpg   Book 5       
6                                   Book 6       
7         70        book_img7.jpg   Book 7       Image of Book 7

SQL Fiddle 中查看结果.

关于MySQL:列出数据及其来自其他表的相关数据(如果可用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23625912/

相关文章:

python - Django 版本的数据库触发器?

php - 数据库分组需要所有其他列

mysql - 数据库用户权限修改后如何恢复?

php - 二维 MySQL 查询?

mysql - 错误代码 1215 : Cannot add foreign key constraint

PHP连接两个表,其中一个表中的两列包含from_ID和to_ID引用另一个表中的id

MySQL - 如何从表中获取结果,以及一个查询中有多少个连接项的计数?

SQL Server 存储过程 - 我可以在一个过程中执行多项操作吗?

c# - 从数据库而不是递归函数检索数据的优化解决方案

mysql - CFUPDATE - 更新数据库