MySQL:要合并的查询
有一张桌子,上面有身份证、物品、咖啡、啤酒、茶、牛奶 已经从其他表中提取了ID和Items,我可以使用case语句使用Items列将item1更新为4 输出看起来MySQL:要合并的查询,mysql,Mysql,有一张桌子,上面有身份证、物品、咖啡、啤酒、茶、牛奶 已经从其他表中提取了ID和Items,我可以使用case语句使用Items列将item1更新为4 输出看起来 ID Item_ID Items Coffee Brewer Tea Milk 1 101 Coffee 1 0 0 0 1 102 Brewer 0 1 0 0
ID Item_ID Items Coffee Brewer Tea Milk
1 101 Coffee 1 0 0 0
1 102 Brewer 0 1 0 0
2 103 Tea 0 0 1 0
3 104 Milk 0 0 0 1
所需输出:我希望基于ID有1,就像前两个记录有coffee和Brewer都应该包含1一样
ID Item_ID Items Coffee Brewer Tea Milk
1 101 Coffee 1 1 0 0 /* Note Brewer value*/
1 102 Brewer 1 1 0 0
2 103 Tea 0 0 1 0
3 104 Milk 0 0 0 1
质疑
这可能会帮助您:
Update All_Items
set coffee = case when Items = 'Coffee' Or id in (select * from (select id from All_Items where items = 'Coffee')t) Then '1' else '0' end,
Brewer= case when Items = 'Brewer' Or id in (select * from (select id from All_Items where items = 'Brewer')t) Then '1' else '0' end,
Tea= case when Items = 'Tea' Or id in (select * from (select id from All_Items where items = 'Tea')t) Then '1' else '0' end,
Milk= case when Items = 'Milk' Or id in (select * from (select id from All_Items where items = 'Milk')t) Then '1' else '0' end;
编辑:
您可以得到两张桌子,并将您的桌子做成这样:
所有项目表
itemID Coffee Brewer Tea Milk
1 1 1 0 0
2 0 0 1 0
3 0 0 0 1
项目表
ID Item_ID
1 101
1 102
2 103
3 104
其中,All_Items.itemID=Items.ID
这样,您可以通过将1设置为按项分隔特定列来轻松更新表。\u id您使用什么逻辑填充表?还有,您的查询是什么请显示您到目前为止所做的查询@诺贝迪奥特大酒店name@MarshallTigerus:哈哈..努力赶上社区的命名惯例趋势..谢谢你的朋友..你是第一个值得欣赏的人!!:到现在为止,我一直在处理我的问题谢谢echo_Me…这是通过加入或任何其他方式实现的,因为我有一百万条记录要更新。谢谢,但这不能满足我的要求。因为我需要所有的物品ID,我不想把101也扔掉。。。。
ID Item_ID
1 101
1 102
2 103
3 104