简体   繁体   English

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

[英]How to select one unique value in mysql for the first row then show nothing for that value, BUT show all the records?

there are 2 mysql table like this 有2个这样的mysql表

table 1 表格1

--id-----config_name --id ----- config_name
--1-----OS --1 -----操作系统
--2-----Control Panel --2 -----控制面板
--3-----Bandwidth --3 -----带宽

table 2 表2

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


Now I want to show them like this, ONLY USING MYSQL. 现在,仅使用MYSQL,我要向他们展示这样的内容。

  1. OS OS

    Windows 2008 = 20.00 Windows 2008 = 20.00

    CentOs 5 = 00.00 CentOs 5 = 00.00

  2. Control Panel 控制面板

    whm/cPanel= 30.00 whm / cPanel = 30.00

    Plesk = 50.00 Plesk = 50.00

IS THIS POSSIBLE? 这可能吗? So now "os" or "control panel" are selected once although if we use group by or join it comes twice. 所以现在选择“ os”或“控制面板”一次,尽管如果我们使用分组依据或联接,它会出现两次。

Using single SQL statement 使用单个SQL语句

Don't use SQL to format output like this. 不要使用SQL这样格式化输出。 You should do it on a client side. 您应该在客户端执行此操作。

Anyway here is a query to do it in SQL only ( SQLFiddle demo ): 无论如何,这是一个仅在SQL中执行的查询( SQLFiddle演示 ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM