2

我有 2 张桌子,第 1 张桌子包含姓名、电子邮件、vegetable_Fruits 和饮料

CREATE TABLE Likes
(
Name varchar(20),
email varchar(20),
Vegetables_Fruits varchar(20),
Drinks varchar(20)
) 

我正在存储喜欢蔬菜、水果和饮料的人的姓名和电子邮件如果一个人喜欢蔬菜,一个人可以有任何一个值,我会将表格更新为

name email 1 0

如果一个人喜欢水果,那么

name email 2 0

如果一个人喜欢喝酒

name email 0 1

插入查询

INSERT INTO Likes(Name,email,Vegetables_Fruits,Drinks)
     VALUES("aaa","aaa@example.com",1,0),("bbb","bbb@example.com",2,0),("ccc","cc@example.com",0,1),("ddd","dd@example.com",2,0),("eee","ee@example.com",2,0),("fff","ff@example.com",1,0),("ggg","gg@example.com",0,1),("hhh","hh@example.com",0,1) 

我有另一个包含电子邮件列表的表

CREATE TABLE emailList
(
Likes_Vegetables varchar(50),
Likes_Fruits varchar(50),
Drinks varchar(50)
)

插入查询

INSERT INTO emailList(Likes_Vegetables,Likes_Fruits,Drinks)
       VALUES("aaa@example.com,ff@example.com","bbb@example.com,dd@example.com,'ee@example.com'","hh@example.com,gg@example.com,cc@example.com")

我想根据第一个表(喜欢)的值更新第二个表(emailList)这个查询是什么

4

3 回答 3

2
update emailList 
set 
Likes_vegetables=(select Group_concat(email) from LIKES where Vegetables_Fruits=1),
Likes_Fruits=(select Group_concat(email) from LIKES where Vegetables_Fruits=2),
Drinks=(select Group_concat(email) from LIKES where Drinks=1)
于 2012-12-14T06:24:04.483 回答
1

我想你在搜索group_concat,也许这样的查询

INSERT INTO emailList(Likes_Vegetables,Likes_Fruits,Drinks)
      values ( (SELECT GROUP_CONCAT(email) FROM Likes WHERE Vegetables_Fruits=1) , (SELECT GROUP_CONCAT(email) FROM Likes WHERE Vegetables_Fruits=2) , (SELECT GROUP_CONCAT(email) FROM Likes WHERE Vegetables_Fruits=0) )
于 2012-12-14T05:57:53.987 回答
-1

我猜这将解决您的问题-

更新 emailList 设置 Likes_Vegetables = concat(Likes_Vegetables,"aaa@example.com"), Likes_Fruits = concat (Likes_Fruits, "ee@example.com"), Drinks = concat (Drinks, "cc@example.com");

于 2012-12-14T06:18:51.953 回答