Mysql:加入查询中的限制

时间:2017-05-10 10:19:34

标签: mysql

以下是我选择所有收藏以及image_name

的查询
 SELECT
    cushbu_art.image_name,
    cushbu_user_collections.collection_name
FROM
    cushbu_art
JOIN cushbu_art_collections ON cushbu_art.id = cushbu_art_collections.art_id
LEFT JOIN cushbu_user_collections ON cushbu_user_collections.id = cushbu_art_collections.collection_id

现在我想从cushbu_art表中仅选择3行(即需要对此JOIN cushbu_art_collections ON cushbu_art.id = cushbu_art_collections.art_id应用限制)

我找到了this问题的解决方案,但我无法理解它的解决方案

更新

当前查询返回此

collection_name image_name

test-collection  test1.jpg
test-collection  test2.jpg
test-collection  test3.jpg
hello-collection hello.jpg

集合名称test-collection重复三次因为子表(cushbu_art)包含一个集合的三个图像 所以我想在cushbu_art.image_name

上应用LIMIT

预期输出

    collection_name image_name

    test-collection  test1.jpg
    test-collection  test2.jpg
    hello-collection hello.jpg

这是我最新的查询

SELECT
    cushbu_art.image_name,
    cushbu_user_collections.collection_name
FROM
    cushbu_art
JOIN cushbu_art_collections ON cushbu_art.id =(
    SELECT
        id
    FROM
        cushbu_art
    WHERE
        cushbu_art.id = cushbu_art_collections.art_id
    LIMIT 1
)
LEFT JOIN cushbu_user_collections ON cushbu_user_collections.id = cushbu_art_collections.collection_id

0 个答案:

没有答案