这是我的表结构:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,lead_id INT NOT NULL
,form INT NOT NULL
,value VARCHAR(20) NOT NULL
,field_no INT NOT NULL
);
INSERT INTO my_table VALUES
( 1 ,1 ,1 ,'John' ,5),
( 2 ,1 ,1 ,'Smith' ,6),
( 3 ,1 ,1 ,'987654321',3),
( 4, 2 ,1 ,'Sam' ,5),
( 5 ,2 ,1 ,'Long' ,6),
( 6 ,2 ,1 ,'42254545' ,3),
( 7 ,3 ,2 ,'23' ,12),
( 8 ,3 ,2 ,'Foo' ,5),
( 9 ,3 ,2 ,'Bar' ,3),
(10 ,4 ,1 ,'Simon' ,5),
(11 ,4 ,1 ,'Rhodes' ,6),
(12 ,4 ,1 ,'659874' ,3);
http://sqlfiddle.com/#!2/77f80
是否可以在一个SELECT语句中选择以下内容:
结果行1:"约翰史密斯"," 987654321"
结果行2:" Sam Long"," 42254545"
结果行3:"" Simon Rhodes"," 659874"
即。如果这是在一行,选择将是:
SELECT CONCAT(姓名,"",Surname)为fullname
,employeeNo来自tableA WHERE form =" 1" group by by lead_id
请帮助
答案 0 :(得分:0)
这只是一个基本的EAV模型
SELECT lead_id
, MAX(CASE WHEN field_no = 5 THEN value END) firstname
, MAX(CASE WHEN field_no = 6 THEN value END) lastname
, MAX(CASE WHEN field_no = 3 THEN value END) telephone
, MAX(CASE WHEN field_no = 12 THEN value END) number
FROM my_table
GROUP
BY lead_id;