我是MySQL新手。我的数据库是客户购买的零售产品或webview数据。示例:
ID Records
R1 {iphone5s,levis,Dell,Dior,amazon}
R2 {levis,Dell,amazon,Starbucks,google glass}
R3 {iphone5s,Dell,Dior,google glass}
R4 {iphone5s,levis,Starbucks}
R5 {iphone5s,levis,Dell,google glass}
我想将此数据存储到数据库中。我将此数据存储为:
ID iphone5s levis Dell Dior amazon Starbucks google glass
R1 1 1 1 1 1 0 0
R2 0 1 1 0 1 1 1
R3 1 0 1 1 0 0 1
R4 1 1 0 0 0 1 0
R5 1 1 1 0 0 0 1
create table retail(ID varchar(50),
iphone5s int,
levis int,
Dell int,
Dior int,
amazon int,
Starbucks int,
googleglass int);
insert into retail
values ('r1',1,1,1,1,1,0,0), ('r2',0,1,1,0,1,1,1);
insert into retail
values ('r3',1,0,1,1,0,0,1),('r4',1,1,0,0,0,1,0),('r5',1,1,1,0,0,0,1);
现在我想检索具有iphone5s的ID并存储ID。类似,我想收集具有每个列名的 id 并存储相应的 id。
使用java我通过以下代码收集列名称:
ResultSet rs = stmt.executeQuery("SELECT * FROM retail");
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
for (int i = 2; i < columnCount + 1; i++ ) {
String name = rsmd.getColumnName(i);
}
现在我想从零售中选择ID
where columnname=1
。
列名每次都应该改变。
来自对问题的评论:
还有其他更好的方式将上述原始数据存储在数据库系统中吗?
当然。您应该将购买的每件商品存储在单独的行中,如下所示:
ID item
-- ------------
R1 iphone5s
R1 levis
R1 Dell
R1 Dior
R1 amazon
R2 levis
R2 Dell
R2 amazon
R2 Starbucks
R2 google glass
...
这使得查询更容易(因为列名是不变的),并且如果您需要添加新产品,则无需更改数据库结构。您始终可以将数据“转换”为您现在拥有的格式,但您不应该以这种方式存储它。
您可以在 MySQL 中使用
UNPIVOT
等效项。现在您可以简单地使用 WHERE brand = 'name' AND is_available = 1
: 进行查询
create table retail(ID varchar(50),
iphone5s int,
levis int,
Dell int,
Dior int,
amazon int,
Starbucks int,
googleglass int);
insert into retail
values ('r1',1,1,1,1,1,0,0), ('r2',0,1,1,0,1,1,1),
('r3',1,0,1,1,0,0,1),('r4',1,1,0,0,0,1,0),('r5',1,1,1,0,0,0,1);
select id
from
(
select
t.id,
c.brand,
case c.brand
when 'iphone5s' then iphone5s
when 'levis' then levis
when 'Dell' then Dell
when 'Dior' then Dior
when 'amazon' then amazon
when 'Starbucks' then Starbucks
when 'googleglass' then googleglass
end as is_available
from retail t
cross join
(
select 'iphone5s' as brand
union all
select 'levis'
union all
select 'Dell'
union all
select 'Dior'
union all
select 'amazon'
union all
select 'Starbucks'
union all
select 'googleglass'
) c
) temp
where brand = 'iphone5s' /* Here your brand/product_name */
and is_available = 1
order by id; /* If needed */
WITH cte AS
(
SELECT ID, Brand, Val
FROM
(
SELECT Id, iphone5s, levis, Dell, Dior, amazon, Starbucks, googleglass
FROM retail
) t
UNPIVOT
(
Val FOR Brand IN (iphone5s, levis, Dell,
Dior, amazon, Starbucks, googleglass)
) AS mrks
)
SELECT ID
FROM cte
WHERE Brand = 'Dior'
AND [val] = 1;