douai2499 2014-09-01 10:06 采纳率: 100%
浏览 544
已采纳

Mysql在复杂查询中使用GROUP_CONCAT加入

我遇到了一个运行查询的问题,我有三个表:

categories

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

photos

| 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       |

portfolios

| 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=‘post’):

| 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放在哪里。

  • 写回答

3条回答 默认 最新

  • douyanyan1123 2014-09-01 10:12
    关注

    Considering, that GROUP_CONCAT() is an aggregate function, you have to use GROUP BY to get desired resultset. You may try this:

    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`
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 脱敏项目合作,ner需求合作
  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密