› MySQL 5.5 Community Server
› MySQL 5.6 Community Server
› Percona Configuration Wizard
› XtraBackup 搭建主从复制
Great Sites on MySQL
› Percona
› MySQL Performance Blog
› Severalnines
推荐管理工具
› Sequel Pro
› phpMyAdmin
推荐书目
› MySQL Cookbook
MySQL 相关项目
› MariaDB
› Drizzle
参考文档
› http://mysql-python.sourceforge.net/MySQLdb.html
qazwsxkevin
V2EX  ›  MySQL

请教, SELECT 有多行结果, mysql 能不能做到按这种指定格式,进行单行输出?

  •  
  •   qazwsxkevin · Dec 25, 2023 · 4019 views
    This topic created in 1017 days ago, the information mentioned may be changed or developed.

    例如,SELECT id FROM record_tbl WHERE id > 7;
    会有 9,14,22,36 结果
    直接想要一个:

    '9','14','22,'36'                    
    

    不知道 mysql 的语法,能不能直接这样输出呢?

    16 replies  •  2023-12-25 16:21:04 +08:00
    iseki
        1
    iseki  
       Dec 25, 2023 via Android
    可以,去聚合函数里看看,肯定有
    thinkershare
        2
    thinkershare  
       Dec 25, 2023
    这种问题,建议你去问 ChatGPT, 100%能告诉你答案
    akira
        3
    akira  
       Dec 25, 2023
    group concat , concat 之类的
    silentsky
        4
    silentsky  
       Dec 25, 2023 via Android
    那必须可以
    bthulu
        5
    bthulu  
       Dec 25, 2023
    可以的, select JSON_ARRAYAGG(id) FROM record_tbl WHERE id > 7;
    KotlinAmai
        6
    KotlinAmai  
       Dec 25, 2023   ❤️ 1
    select group_concat(concat('\'', id, '\''))
    from (select 9 as id
    union all
    select 14 as id
    union all
    select 22 as id
    union all
    select 36 as id) t;
    xwayway
        7
    xwayway  
       Dec 25, 2023
    @actar 你可真是可小天才
    shakoon
        8
    shakoon  
       Dec 25, 2023
    需求描述不清晰。你是想要最大的那个吗? SELECT MAX(id) FROM record_tbl WHERE id > 7
    tedzhou1221
        9
    tedzhou1221  
       Dec 25, 2023
    行转列
    tedzhou1221
        10
    tedzhou1221  
       Dec 25, 2023
    ```sql
    select group_concat(concat('"',user_id,'"'))
    from (select user_id
    from sys_user where sys_user.user_id > 100) as `a`
    ```
    retanoj
        11
    retanoj  
       Dec 25, 2023   ❤️ 2
    你需要的是 group_concat 和 concat 函数

    select GROUP_CONCAT(id) from record_tbl where id > 7; 会输出

    9,14,22,36

    如果需要引号就

    select GROUP_CONCAT( concat( "'", id, "'" ) ) from record_tbl where id > 7;
    chinni
        12
    chinni  
       Dec 25, 2023   ❤️ 2
    group_concat 注意默认长度限制 1024
    另外 这类逻辑 如果只是简单查询 推荐用程序逻辑去处理
    sitboy
        13
    sitboy  
       Dec 25, 2023
    是不是不准贴 chatgpt 的答案了啊?
    montaro2017
        14
    montaro2017  
       Dec 25, 2023
    SELECT
    GROUP_CONCAT( ID )
    FROM
    ( SELECT 9 AS ID UNION SELECT 14 AS ID UNION SELECT 22 AS ID UNION SELECT 36 AS ID ) T

    SmartTom
        15
    SmartTom  
       Dec 25, 2023
    你这太面向结果编程了。可以加个虚拟列 然后在外层地 group_concat(id),对虚拟列 group by (v)
    SmartTom
        16
    SmartTom  
       Dec 25, 2023
    @SmartTom
    with t as (SELECT id , 'vt' as v FROM record_tbl WHERE id > 7)
    select GROUP_CONCAT(t.id) from t GROUP BY t.v
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2726 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 06:09 · PVG 14:09 · LAX 23:09 · JFK 02:09
    ♥ Do have faith in what you're doing.