简体   繁体   English

MySQL选择具有相同值的行并输出到HTML CSS /表

[英]MySQL Select rows with same value and output to HTML css/table

I would really appreciate your help on this: 非常感谢您的帮助:

Item_ID Buy_User    Buy_Date    Buy_Qty 
00001       Adam        01/02/2013  1
00003       John        01/01/2013  2
00004       Peter       02/01/2013  1
00001       Nial        01/01/2013  1

Above is an example of my table. 上面是我的桌子的一个例子。 What I need is to sql query the Item_ID column, and display each Item_ID only once - but display each of the user's who have bought that product. 我需要的是sql查询Item_ID列,并且只显示每个Item_ID一次-但显示购买该产品的每个用户。

I need the query to be outputted in HTML/css. 我需要在HTML / css中输出查询。

For example my html page may look like: 例如,我的html页面可能如下所示:

Item 1: Adam bought 1. Nial bought 1. 物品1:亚当买了1.尼尔买了1。

Item 2: (would be hidden as no purchases). 项目2 :(将被隐藏,因为没有购买)。

Item 3: John bought 2. 物品3:约翰买了2。

Item 4: Peter bought 1. 物品4:彼得买了1。

I'm not quite sure how to achieve this, I've been googling around and found multiple mentions of sub selects, but i'm not sure how i'd get it to display each row? 我不太确定如何实现此目的,我一直在谷歌上搜索,发现多次提到子选择,但是我不确定如何显示每一行?

Help?! 救命?!

Many thanks. 非常感谢。

Is this what you're looking for? 这是您要找的东西吗? It uses GROUP_CONCAT to group the rows and CONCAT to build the strings: 它使用GROUP_CONCAT对行进行分组,并使用CONCAT来构建字符串:

SELECT Item_Id, GROUP_CONCAT(CONCAT(Buy_User,' bought ',Buy_Qty) SEPARATOR ', ') ITEMS
FROM Items
GROUP BY Item_Id

Here is the SQL Fiddle . 这是SQL Fiddle

And here are the results: 结果如下:

ITEM_ID   ITEMS
00001     Adam bought 1, Nial bought 1
00003     John bought 2
00004     Peter bought 1

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

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