mysql Complex select with just one table

Here is my table structure:

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

Is it possible to select the following in just one SELECT statement: RESULT ROW 1: "John Smith", "987654321" RESULT ROW 2: "Sam Long", "42254545" RESULT ROW 3: "Simon Rhodes", "659874"

ie. If this was on ONE row, the select would be: SELECT CONCAT(Name," ", Surname) as fullname, employeeNo from tableA WHERE form = "1" group by lead_id

Please help

Answers


This is just a basic EAV model

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;

http://sqlfiddle.com/#!2/ea3b1f/1


Need Your Help

In the context of rails, how can I order columns by the second?

ruby-on-rails ruby postgresql

I am working with rails and postgresql. I have a created_at timestamp on a model. I am in a situation where I need to order the results by second. I can't seem to get rails todo this.

How to make a background text appear under youtube video?

html css

There is a horizontally centered youtube video on the page, but the problem is that on mobile devices (phones/tablets) it takes time before the the video loads (sometimes 1.5 secs). So during this ...