SQL Query从同一列中获取数据?

时间:2014-07-17 10:13:32

标签: php mysql sql database

如何根据a_和group_id各自的data_id从单个列中获取数据?

--------------------------------------------
id  |  group_id  |  data_id  |value 
--------------------------------------------
1          20          15        Product1
2          20           1         Name1
3          20           2       Location1
4          20           3        Price1  
5          20           4          Tag1
6          21          15         Product2
.
.
. and so on 

疾病输出是:

--------------------------------------------
Product  |  Name  |  Location  |  Price  |  Tag
--------------------------------------------
Product1    Name1     Location1     Price1    Tag1
Product2    Name2     Location2     Price2    Tag2

如果data_id = 15并且值='产品1'则更新那么所有与Product1相关的值都应该从数据库中提取,如果data_id = 15 AND value =' Product2'然后应该获取与Product2相关的所有值

3 个答案:

答案 0 :(得分:1)

可以做到,但这根本不是一个好的设计。您应该有5个表,每个data_id一个,然后使用连接查询来检索您想要的内容。

但这是对你糟糕设计的查询:

select product, name, ... from
(select group_id as g1 value as product from bad_table where data_id = 15),
(select group_id as g2 value as name from bad_table where data_id = 1), 
...
where g1 = g2 and ...;

答案 1 :(得分:1)

您可以使用GROUP_CONCAT()功能实现此目的。但是@Hamid Alaei said这不是一个好的设计。

如果data_id对于任何值保持相同,例如价值"产品" data_id总是15,那么你可以试试这个:

SELECT 
      GROUP_CONCAT(CASE WHEN `data_id` = 15 
                        THEN `value` ELSE NULL END) AS Product
      ,GROUP_CONCAT(CASE WHEN `data_id` = 1 
                         THEN `value` ELSE NULL END) AS Name
      ,GROUP_CONCAT(CASE WHEN `data_id` = 2 
                         THEN `value` ELSE NULL END) AS Location
      ,GROUP_CONCAT(CASE WHEN `data_id` = 3
                         THEN `value` ELSE NULL END) AS Price
      ,GROUP_CONCAT(CASE WHEN `data_id` = 4
                         THEN `value` ELSE NULL END) AS Tag
FROM Table1
GROUP BY `group_id`;

否则试试这个:

SELECT 
      GROUP_CONCAT(CASE WHEN `value` LIKE 'Product%' 
                        THEN `value` ELSE NULL END) AS Product
      ,GROUP_CONCAT(CASE WHEN `value` LIKE 'Name%' 
                         THEN `value` ELSE NULL END) AS Name
      ,GROUP_CONCAT(CASE WHEN `value` LIKE 'Location%' 
                         THEN `value` ELSE NULL END) AS Location
      ,GROUP_CONCAT(CASE WHEN `value` LIKE 'Price%' 
                         THEN `value` ELSE NULL END) AS Price
      ,GROUP_CONCAT(CASE WHEN `value` LIKE 'Tag%' 
                         THEN `value` ELSE NULL END) AS Tag
FROM Table1
GROUP BY `group_id`

结果(给定表格):

|  PRODUCT |   NAME |  LOCATION |  PRICE |    TAG |
|----------|--------|-----------|--------|--------|
| Product1 |  Name1 | Location1 | Price1 |   Tag1 |
| Product2 | (null) |    (null) | (null) | (null) |

请参阅this SQLFiddle

答案 2 :(得分:0)

select (select product from table group by product) as product , 
(select name from table group by name) as name,
(select location from table group by location) as location,
(select price from table group by price) as price , 
(select tag from table group by tag) as tag from table