![](/img/trans.png)
[英]How to SELECT two records for each unique column value as one row in MySQL?
[英]How to select one unique value in mysql for the first row then show nothing for that value, BUT show all the records?
有2個這樣的mysql表
--id ----- config_name
--1 -----操作系統
--2 -----控制面板
--3 -----帶寬
id-config_id --- config_Option_name ---價格
1 -------- 1 ------------- Windows 2008 -------- 20.00
2 -------- 1 ------------- CentOs 5 ---------------- 0.00
3 -------- 2 ------------- whm / cPanel ----------- 30.00
4 -------- 2 ------------- Plesk ------------------- 50.00
現在,僅使用MYSQL,我要向他們展示這樣的內容。
- OS
Windows 2008 = 20.00
CentOs 5 = 00.00- 控制面板
whm / cPanel = 30.00
Plesk = 50.00
這可能嗎? 所以現在選擇“ os”或“控制面板”一次,盡管如果我們使用分組依據或聯接,它會出現兩次。
使用單個SQL語句
不要使用SQL這樣格式化輸出。 您應該在客戶端執行此操作。
無論如何,這是一個僅在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.