mysql - 如何在 mysql 中为第一行选择一个唯一值然后不显示该值,但显示所有记录?

标签 mysql

有2个这样的mysql表

表1

--id-----config_name
--1-----OS
--2-----Control Panel
--3-----Bandwidth

表2

id -- config_id--- config_Option_name --- Price
1--------1-------------Windows 2008--------20.00
2--------1-------------CentOs 5----------------0.00
3--------2-------------whm/cPanel-----------30.00
4--------2-------------Plesk-------------------50.00


现在我想像这样展示它们,只使用 MYSQL。

  1. OS

    Windows 2008 = 20.00

    CentOs 5 = 00.00

  2. Control Panel

    whm/cPanel= 30.00

    Plesk = 50.00

这可能吗?所以现在“os”或“control panel”被选择了一次,尽管如果我们使用 group by 或 join,它会出现两次。

使用单个 SQL 语句

最佳答案

不要使用 SQL 来格式化这样的输出。您应该在客户端执行此操作。

无论如何,这是一个仅在 SQL 中执行的查询 (SQLFiddle demo):

select cfgN from
(
   select concat('\t',
                 CONFIG_OPTION_NAME,
                 '=',FORMAT(Price, 2) ) as cfgN, 
                 config_id,
                 t2.ID,
                 CONFIG_OPTION_NAME,
                 Price 
   from Table2 t2
   join Table1 t1 on (t2.config_id=t1.id)

   union all

   select concat(cast(id as char),
                 '. ',CONFIG_NAME) as cfgN, 
          id config_id,
          null ID,
          CONFIG_NAME, 
          null Price 
   from table1 

) t3 order by config_id,id

关于mysql - 如何在 mysql 中为第一行选择一个唯一值然后不显示该值,但显示所有记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13717682/

相关文章:

php - INSERT INTO 无法正常工作 MySQL

php - 根据 ROW 插入数据

mysql - 使用 java 和 junit 测试测试 mysql etl

MySQL 查询到 CSV

mysql - 在 mysql 中创建一个 "secondary ID"

mysql - 通过 ssh 隧道连接到 MySQL 到本地主机

mysql - 使用 1 个 MySQL 表 Access 要求凭据

java - 提交事务后数据未更新

mysql - 使用 knexjs 插入忽略

phpMyAdmin 导致连接失败