簡體   English   中英

mysql JOIN與GROUP_CONCAT在復雜的查詢中

[英]Mysql JOIN with GROUP_CONCAT in a complex query

我遇到了運行查詢的問題。 我有三張桌子:

分類

| id  | name       | 
| --- | ---------- | 
| 1   | Logo       | 
| 2   | Poster     |
| 3   | Stationary | 
| 4   | Web        |
| 5   | Projects   | 
| 6   | Sporadic   | 

相片

| id  | portid | image            | preveiw |
| --- | ------ | ---------------- | ------- |
| 171 | 30     | a30preview.jpg   | 1       |
| 172 | 30     | b30.jpg          |         |
| 173 | 30     | c30.jpg          |         |
| 174 | 32     | a32preview.jpg   | 1       |
| 175 | 32     | b32.jpg          |         |
| 176 | 33     | a33preview.jpg   | 1       |
| 179 | 33     | b33.jpg          |         |
| 180 | 41     | a41preview.jpg   | 1       |

作品集

| id  | catid  | type  | title        | text         | date   |
| --- | ------ | ----- | ------------ | ------------ | ------ |
| 30  | 2      | M     | xxxxxxxx     | xxxxxxxx     | xxxxxx |
| 32  | 2      | M     | xxxxxx       | xxxxxx       | xxxxxx |
| 33  | 2      | L     | xxxxxxxxxx   | xxxxxxxxxx   | xxxxxx |
| 41  | 1      | L     | xxxx         | xxxx         | xxxxxx |
| 45  | 2      | L     | xxxxx        | xxxxx        | xxxxxx | <-(This record has no image in 'photos' table, so it's not in 'output')

我想獲取這些記錄作為輸出(其中name ='Poster')

| id  | catid  | name     | type  | title        | text         | date   | image   <-(first image is the one that has 'photos.preview' = 1)|
| --- | ------ | -------- | ----- | ------------ | ------------ | ------ | ---------------------------------- |
| 30  | 2      | Poster   | M     | xxxxxxxx     | xxxxxxxx     | xxxxxx | a30preview.jpg, b30.jpg, c30.jpg   |
| 32  | 2      | Poster   | M     | xxxxxx       | xxxxxx       | xxxxxx | a32preview.jpg, b32.jpg            |
| 33  | 2      | Poster   | L     | xxxxxxxxxx   | xxxxxxxxxx   | xxxxxx | a33preview.jpg, b33.jpg            |

我已經試過這個SQL語句:

select * from `portfolios` 
   inner join `categories` on `portfolios`.`catid` = `categories`.`id` 
   inner join `photos` on `portid` = `portfolios`.`id` 
where `categories`.`name` = "Poster"

當然這是行不通的,因為我不知道應該在哪里放置GROUP_CONCAT 任何想法 ?

SELECT portfolios.*, categories.*, GROUP_CONCAT(photos.image) AS image
FROM `portfolios` 
inner join `categories` on `portfolios`.`catid` = `categories`.`id` 
inner join `photos` on `portid` = `portfolios`.`id` 
where `categories`.`name` = "Poster"
GROUP BY portfolios.id

考慮到GROUP_CONCAT()是一個聚合函數,您必須使用GROUP BY來獲得所需的結果集。 您可以嘗試以下方法:

SELECT
    `portfolios`.*,
    `categories`.*,
    GROUP_CONCAT(
        `photos`.`image`
            ORDER BY `photos`.`preveiw` DESC
            SEPARATOR ', '
    ) as `image`
FROM
   `portfolios` 
       INNER JOIN `categories` ON `portfolios`.`catid` = `categories`.`id` 
       INNER JOIN `photos` ON `portid` = `portfolios`.`id` 
WHERE
    `categories`.`name` = 'Poster'
GROUP BY
    `portfolios`.`id`

如果您對列進行拼寫而不是使用*會容易得多:

select p.id, p.catid, c.name, p.type, p.title
     , p.text, p.date. group_concat(ph.image order by case when ph.previw = 1 
                                                           then 0 else 1 
                                                      end) as images 
from `portfolios` p 
join `categories` c
    on p.`catid` = c.`id` 
join `photos` ph
    on ph.`portid` = p.`id` 
where c.`name` = "Poster"
group by p.id, p.catid, c.name, p.type, p.title
       , p.text, p.date 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM