![](/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.