Query by Value in Adjacent Column in MySQL - mysql

I believe all questions of this nature start out this way...
I have two tables: fields and data. Fields describes the column names of a (non-existant) table and data contains the data of that (non-existant) table. Like so...
Fields:
+----+---------+-------------+---------------+-------+----------+
| ID | F_ORDER | NAME | LABEL | VALUE | TYPE |
+----+---------+-------------+---------------+-------+----------+
| 1 | 1 | IS_EMPLOYEE | Region | | checkbox |
| 2 | 3 | EM_AVATAR | Avatar | | avatar |
| 3 | 4 | EM_JOBTITLE | Job Title | | text |
| 4 | 5 | EM_COMPANY | Company | | text |
| 5 | 6 | EM_PHONE | Phone | | text |
| 6 | 2 | EM_ORDER | Display Order | 5 | text |
+----+---------+-------------+---------------+-------+----------+
Data:
+-----+----------+---------+--------------------------------------+
| ID | FIELD_ID | USER_ID | VALUE |
+-----+----------+---------+--------------------------------------+
| 5 | 1 | 1 | YES |
| 6 | 2 | 1 | |
| 7 | 3 | 1 | Owner |
| 8 | 4 | 1 | Acme, Inc. |
| 9 | 5 | 1 | 123-456-7987 |
| 150 | 5 | 31 | 123-623-5555 |
| 149 | 4 | 31 | Acme, Inc. |
| 148 | 3 | 31 | Sales and Customer Support |
| 147 | 2 | 31 | |
| 146 | 1 | 31 | YES |
| 26 | 1 | 6 | NO |
| 27 | 2 | 6 | http://example.com/avi/avi.jpeg |
| 28 | 3 | 6 | CEO |
| 29 | 4 | 6 | Acme |
| 30 | 5 | 6 | (123) 734-5555 |
| 31 | 1 | 7 | NO |
| 32 | 2 | 7 | http://example.com/avi/avi.jpeg |
| 33 | 3 | 7 | VP, Services |
| 34 | 4 | 7 | Acme |
| 35 | 5 | 7 | (913) 963-5555 |
| 36 | 1 | 14 | NO |
| 37 | 2 | 14 | http://example.com/avi/avi.jpeg |
| 38 | 3 | 14 | Senior Accountant |
| 39 | 4 | 14 | Acme |
| 40 | 5 | 14 | (123) 213-5555 |
| 41 | 1 | 10 | NO |
| 42 | 2 | 10 | http://example.com/avi/avi.jpeg |
| 43 | 3 | 10 | President |
| 44 | 4 | 10 | Acme |
| 45 | 5 | 10 | (123) 734-5555 |
| 46 | 1 | 12 | NO |
| 47 | 2 | 12 | http://example.com/avi/avi.jpeg |
| 48 | 3 | 12 | Services Supervisor |
| 49 | 4 | 12 | Acme |
| 50 | 5 | 12 | (123) 573-5555 |
| 51 | 1 | 11 | NO |
| 52 | 2 | 11 | http://example.com/avi/avi.jpeg |
| 53 | 3 | 11 | Operations Supervisor |
| 54 | 4 | 11 | Acme |
| 55 | 5 | 11 | (123) 259-5555 |
| 56 | 1 | 8 | NO |
| 57 | 2 | 8 | http://example.com/avi/avi.jpeg |
| 58 | 3 | 8 | General Information |
| 59 | 4 | 8 | Acme |
| 60 | 5 | 8 | (123) 213-5555 |
| 61 | 1 | 9 | NO |
| 62 | 2 | 9 | http://example.com/avi/avi.jpeg |
| 63 | 3 | 9 | VP, Sales |
| 64 | 4 | 9 | Acme |
| 65 | 5 | 9 | (123) 210-5555 |
+-----+----------+---------+--------------------------------------+
The basic verbiage of the query I'm looking for is: I want all information for all people who are employees (IS_EMPLOYEE = "YES") and ordered by their display order column (EM_ORDER).
My query so far gets me nowhere. I get results a little like this:
+--------+------------+------------+-------+---------+-------+
ID FIELD_ID NAME LABEL TYPE VALUE
+--------+------------+------------+-------+---------+-------+
7 1 IS_EMPLOYEE Region checkbox YES
+--------+------------+------------+-------+---------+-------+
What I need are results like this:
+-------+------------+---------+-----------+----------+-------------+--------+
USER_ID IS_EMPLOYEE EM_AVATAR EM_JOBTITLE EM_COMPANY EM_PHONE EM_ORDER
+-------+------------+---------+-----------+----------+-------------+--------+
6 YES http:// CEO Acme 123-123-555 5
+-------+------------+---------+-----------+----------+-------------+--------+
And of course I'm trying to get it all back into PHP as a usable array ($results['user_id']['jobtitle'] etc.). I could just get everything and work through it with PHP, but I'm trying to learn MySQL and I think this is a faster method than doing several foreach blocks ... although I could be wrong.
Thanks in advance for any help.

Your data table is organized in the Entity-Attribute-Value manner, which is not a valid way of storing relational data. It's no wonder that it's awkward to retrieve it with SQL into a conventional row with one column per attribute.
To do this with SQL, you basically need to do a pivot query. This is hard to write and terribly inefficient to execute.
You're better off fetching all the data for employees:
SELECT e.*, f.label
FROM Data AS is_employee
JOIN Data AS e USING (user_id)
JOIN Fields AS f ON e.field_id = f.id
WHERE (is_employee.field_id, is_employee.value) = (1, 'Yes');
And then reorganizing it into a collection of associative arrays in PHP code:
while ($row = $stmt->fetch()) {
$data[ $row['user_id'] ][ $row['label'] ] = $row['value'];
}

This is basically a PIVOT but MySQL does not have a PIVOT function so you can replicate it using an aggregate function and a CASE statement.
A Static version is when you know all of the values that you want to transform into columns (these are your field names):
select
d.user_id,
max(case when f.name = 'IS_EMPLOYEE' then d.value else null end) IS_EMPLOYEE,
max(case when f.name = 'EM_AVATAR' then d.value else null end) EM_AVATAR,
max(case when f.name = 'EM_JOBTITLE' then d.value else null end) EM_JOBTITLE,
max(case when f.name = 'EM_COMPANY' then d.value else null end) EM_COMPANY,
max(case when f.name = 'EM_PHONE' then d.value else null end) EM_PHONE,
max(case when f.name = 'EM_ORDER' then d.value else null end) EM_ORDER
from data d
left join fields f
on f.id = d.FIELD_ID
group by d.user_id
See SQL Fiddle with Demo
Result:
| USER_ID | IS_EMPLOYEE | EM_AVATAR | EM_JOBTITLE | EM_COMPANY | EM_PHONE | EM_ORDER |
---------------------------------------------------------------------------------------------------------------------------------
| 1 | YES | (null) | Owner | Acme, Inc. | 123-456-7987 | (null) |
| 6 | NO | http://example.com/avi/avi.jpeg | CEO | Acme | (123) 734-5555 | (null) |
| 7 | NO | http://example.com/avi/avi.jpeg | VP, Services | Acme | (913) 963-5555 | (null) |
| 8 | NO | http://example.com/avi/avi.jpeg | General Information | Acme | (123) 213-5555 | (null) |
| 9 | NO | http://example.com/avi/avi.jpeg | VP, Sales | Acme | (123) 210-5555 | (null) |
| 10 | NO | http://example.com/avi/avi.jpeg | President | Acme | (123) 734-5555 | (null) |
| 11 | NO | http://example.com/avi/avi.jpeg | Operations Supervisor | Acme | (123) 259-5555 | (null) |
| 12 | NO | http://example.com/avi/avi.jpeg | Services Supervisor | Acme | (123) 573-5555 | (null) |
| 14 | NO | http://example.com/avi/avi.jpeg | Senior Accountant | Acme | (123) 213-5555 | (null) |
| 31 | YES | (null) | Sales and Customer Support | Acme, Inc. | 123-623-5555 | (null) |
If you have an unknown number of values to turn into columns, then you can use a prepared statement to generate the sql dynamically.
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when f.name = ''',
name,
''' then d.value end) AS ',
name
)
) INTO #sql
FROM fields;
SET #sql = CONCAT('SELECT d.user_id, ', #sql, '
from data d
left join fields f
on f.id = d.FIELD_ID
GROUP BY d.user_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
See SQL Fiddle with Demo
Both will produce the same result.
You can then add a WHERE clause to filter out any of the unneeded rows.

Related

Mysql Row To Column Select Specific Data

I have column user and rating.
SELECT rating.idUser, user.nmUser, rating.idBengkel, rating.nilai FROM `rating`
JOIN user on rating.idUser = user.idUser
WHERE rating.idBengkel=1 or rating.idBengkel=2
Result :
+--------+---------------------------+-----------+-------+
| idUser | nmUser | idBengkel | nilai |
+--------+---------------------------+-----------+-------+
| 10 | Hudson mas77 | 1 | 5 |
| 11 | Vina Nurfadzilah | 1 | 5 |
| 12 | Angelica Amartya | 1 | 5 |
| 15 | Syahrul K | 1 | 4 |
| 27 | Ashar Murdihastomo | 1 | 5 |
| 28 | Eril Obeit Choiri | 1 | 2 |
| 29 | Ariyadi | 1 | 3 |
| 30 | Robertus Dwian Augusta | 1 | 4 |
| 31 | Irfan Setiaji | 1 | 4 |
| 33 | Baby Ayuna | 1 | 5 |
| 9 | Nur k hamid | 2 | 5 |
| 10 | Hudson mas77 | 2 | 5 |
| 13 | Yuana Putra | 2 | 4 |
| 14 | Nanda Aulia Irza Ramadhan | 2 | 4 |
| 26 | taufiq rahman | 2 | 5 |
| 27 | Ashar Murdihastomo | 2 | 5 |
| 28 | Eril Obeit Choiri | 2 | 5 |
| 30 | Robertus Dwian Augusta | 2 | 4 |
| 44 | halim budiono | 2 | 1 |
+--------+---------------------------+-----------+-------+
When i try to get similar records using this query
SELECT rating.idUser, user.nmUser FROM rating
JOIN user
ON rating.idUser = user.idUser
WHERE rating.idBengkel = 1 and rating.idUser
IN (SELECT rating.idUser from rating WHERE rating.idBengkel = 2)
ORDER by idUser
Result :
+-----------+------------------------+
| idUser | nmUser |
+-----------+------------------------+
| 10 | Hudson mas77 |
| 27 | Ashar Murdihastomo |
| 28 | Eril Obeit Choiri |
| 30 | Robertus Dwian Augusta |
+-----------+------------------------+
The result work fine, but I want show column 'nilai' as ItemX and ItemY. Those are user similar data. In this case I have 4 similar user who rate on idBengkel=1 and idBengkel=2 as the results above. I want it like the table below.
+--------+------------------------+-------+-------+
| idUser | nmUser | ItemX | ItemY |
+--------+------------------------+-------+-------+
| 10 | Hudson mas77 | 5 | 5 |
| 27 | Ashar Murdihastomo | 5 | 5 |
| 28 | Eril Obeit Choiri | 2 | 5 |
| 30 | Robertus Dwian Augusta | 4 | 4 |
+--------+------------------------+-------+-------+
I need solution for this and i was trying with this solution in https://stackoverflow.com/a/7976379/12396302 but it resulting more than one row. Please help me, I cant implement that query's solution. Regards!
I think you need below query -
SELECT rating.idUser,
user.nmUser,
MAX(CASE WHEN rating.idBengkel = 1 THEN rating.nilai END) ItemX,
MAX(CASE WHEN rating.idBengkel = 2 THEN rating.nilai END) ItemY,
FROM `rating`
JOIN user on rating.idUser = user.idUser
WHERE rating.idBengkel IN (1, 2)
GROUP BY rating.idUser,
user.nmUser

How to showing Group in Table ( Angular 6 above)

I want to ask, if I have a flat sales data in Json format :
[ {Amir,Adidas,36,2},{Amir,Nike,30,2},{Amir,Puma,37,3},
{{Meta,Adidas,41,1},{Meta,Nike,33,2},Meta, Puma,42,4},{Meta, Puma,43,3}
{Samsi,Adidas,40,1},Samsi,Nike,31,1},{Bulma,Puma,41,3} ]
and the data already sorted by Sales Name, Product Name, shoes no, and Name.
+---------------------+--------------+--------------+-------------------+
| Nama | Adidas | Nike | Puma |
| +----+----+----+----+----+----+----+----+----+----+
| | 36 | 40 | 41 | 30 | 31 | 33 | 37 | 41 | 42 | 43 |
+---------------------+----+----+----+----+----+----+----+----+----+----+
| Amir | 2 | | | 2 | | | 3 | | | |
| Meta | | | 1 | | | 2 | | | 4 | 3 |
| Samsi | | 1 | | | 1 | | | 3 | | |
+---------------------+----+----+----+----+----+----+----+--- +----+----+
How to make HTML table to present the data, like above.
I am using angular 6 as front end.
Thank You

Multiple table join with possible conditional matches

I have several tables that I combine in an application I'm creating in PHP that essentially creates a check list. I realize I could solve this problem using a conditional in PHP, but am curious if MySQL is capable of accomplishing this and if so, how? Specifically, I have four tables which are queried using the following statement:
SELECT
cl_status.status,
users.user_first,
cl_status.date AS status_date,
cl_status.id AS status_id,
cl_status.criteria_id,
cl_criteria.id AS cid,
cl_criteria.description AS description
FROM cl_criteria
LEFT JOIN cl_lists
ON cl_criteria.cl_id = cl_lists.id
RIGHT JOIN cl_status
ON cl_criteria.id = cl_status.criteria_id
LEFT JOIN users
ON cl_status.user_id = users.user_id
WHERE cl_lists.id = '1'
Table one - cl_lists:
+----+------------------+------------+------------+-------+
| id | title | date | comp_level | owner |
+----+------------------+------------+------------+-------+
| 1 | Newcomer's guide | 1452473606 | 1 | 1 |
+----+------------------+------------+------------+-------+
Table two - cl_assign:
+----+-------+-------+------------+
| id | cl_id | owner | date |
+----+-------+-------+------------+
| 1 | 1 | 1 | 1455843514 |
+----+-------+-------+------------+
Table three - cl_status:
+----+-------------+---------+-------------+------------+--------+----------+
| id | criteria_id | user_id | description | date | status | comments |
+----+-------------+---------+-------------+------------+--------+----------+
| 2 | 66 | 1 | | NULL | 1 | NULL |
| 15 | 65 | 1 | | 1455842197 | 5 | NULL |
| 16 | 67 | 1 | | 1455842201 | 5 | NULL |
| 17 | 68 | 1 | | 1455842203 | 5 | NULL |
| 18 | 69 | 1 | | 1455842217 | 0 | NULL |
| 19 | 70 | 1 | | 1455842222 | 5 | NULL |
| 20 | 72 | 1 | | 1455842237 | 1 | NULL |
| 21 | 71 | 1 | | 1455842234 | 0 | NULL |
| 22 | 73 | 1 | | 1455842246 | 5 | NULL |
| 23 | 76 | 1 | | 1455842249 | 5 | NULL |
| 24 | 77 | 1 | | 1455842268 | 5 | NULL |
| 25 | 78 | 152 | | 1455854420 | 3 | NULL |
| 26 | 81 | 1 | | 1455843660 | 5 | NULL |
+----+-------------+---------+-------------+------------+--------+----------+
Table four - users:
+---------+------------+
| user_id | user_first |
+---------+------------+
| 1 | Mark |
| 2 | Test |
+---------+------------+
Ideally, I'd like the join to look like this:
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
| status | user_first | status_date | status_id | criteria_id | cid | description |
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
| 5 | Mark | 1455842197 | 15 | 65 | 65 | Tour of facility |
| 5 | Mark | 1455842201 | 16 | 67 | 67 | Tax forms |
| 5 | Mark | 1455842203 | 17 | 68 | 68 | 2 forms of ID |
| 0 | Mark | 1455842217 | 18 | 69 | 69 | Benefits | |
| 5 | Mark | 1455842246 | 22 | 73 | 73 | Intro to policies |
| 5 | Mark | 1455842249 | 23 | 76 | 76 | Setup email account |
| NULL | NULL | NULL | NULL | 78 | 78 | Setup Computer account |
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
However, it looks like this:
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
| status | user_first | status_date | status_id | criteria_id | cid | description |
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
| 5 | Mark | 1455842197 | 15 | 65 | 65 | Tour of facility |
| 5 | Mark | 1455842201 | 16 | 67 | 67 | Tax forms |
| 5 | Mark | 1455842203 | 17 | 68 | 68 | 2 forms of ID |
| 0 | Mark | 1455842217 | 18 | 69 | 69 | Benefits |
| 5 | Mark | 1455842246 | 22 | 73 | 73 | Intro to policies |
| 5 | Mark | 1455842249 | 23 | 76 | 76 | Setup email account |
| 3 | Temp | 1455854420 | 25 | 78 | 78 | Setup Computer account |
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
Is there a way to apply the conditional before the join? Or another way to accomplish the result set that I want?
EDIT
This is a screenshot of what the application looks like:
The criteria table will include steps of every checklist I have. The list table is a list of the various checklists. The status table allows every user (such as Mark, or Test) to look at the same checklist and complete it as if it was a separate document. It also populates the date/time that the item was updated by that user.
I suspect that the RIGHT JOIN you have in your query is causing the records you want to appear to be filtered out. Remember that t1 RIGHT JOIN t2 is the same as t2 LEFT JOIN t1, meaning that t1 will lose any record which does not appear in t2, with t2 keeping all its records. Try this:
SELECT cl_status.status, users.user_first, cl_status.date AS status_date,
cl_status.id AS status_id, cl_status.criteria_id, cl_criteria.id AS cid,
cl_criteria.description AS description
FROM cl_criteria LEFT JOIN cl_lists
ON cl_criteria.cl_id = cl_lists.id
LEFT JOIN cl_status
ON cl_criteria.id = cl_status.criteria_id
LEFT JOIN users
ON cl_status.user_id = users.user_id
WHERE cl_lists.id = '1'

Group rows with sum and join (Build summary)

There is a rowset like this:
| ID | OP_CODE | OWNER | MEASURE | COUNT |
|----|-------------|-------|----------|-------|
| 1 | Operation 1 | 1 | Geometry | 42 |
| 2 | Operation 1 | 1 | Geometry | 48 |
| 3 | Operation 1 | 1 | Vacuum | 29 |
| 4 | Operation 1 | 1 | Electro | 14 |
| 5 | Operation 1 | 2 | Geometry | 87 |
| 6 | Operation 1 | 2 | Geometry | 112 |
| 7 | Operation 1 | 2 | Vacuum | 78 |
| 8 | Operation 1 | 3 | Vacuum | 56 |
| 9 | Operation 1 | 3 | Electro | 78 |
I want to group rows by Owner and merge/join other Measures (values of column MEASURE) with sum of column Count to this result like this:
| OWNER | GEOMETRY_CNT | VACUUM_CNT | ELECTRO_CNT | TOTAL_CNT |
|-------|--------------|------------|-------------|-----------|
| 1 | 90 | 29 | 14 | 133 |
| 2 | 199 | 78 | (null) | 277 |
| 3 | (null) | 56 | 78 | 134 |
In this case Geometry_cnt, Vacuum_cnt, Electro_cnt is a sum of corresponding values in first table:
Owner_1_Geometry_cnt=42+48=90;
Owner_1_Vacuum_cnt=29;
Owner_1_Electro_cnt=14;
Owner_1_TOTAL=29+14+90=133;
How can I get this rowset?
SQL Fiddle
Try this:
SELECT a.OWNER, SUM(IF(a.MEASURE = 'Geometry', a.COUNT, 0)) GEOMETRY_CNT,
SUM(IF(a.MEASURE = 'Vacuum', a.COUNT, 0)) VACUUM_CNT,
SUM(IF(a.MEASURE = 'Electro', a.COUNT, 0)) ELECTRO_CNT,
SUM(a.COUNT) TOTAL_CNT
FROM operations_schedule a
GROUP BY a.OWNER
Check the SQL FIDDLE DEMO
OUTPUT
| OWNER | GEOMETRY_CNT | VACUUM_CNT | ELECTRO_CNT | TOTAL_CNT |
|-------|--------------|------------|-------------|-----------|
| 1 | 90 | 29 | 14 | 133 |
| 2 | 199 | 78 | 0 | 277 |
| 3 | 0 | 56 | 78 | 134 |

Mysql query to convert table from long format to wide format

I have a table called ContactAttrbiutes which contains a list of each contacts' attributes. The kind of data stored for these contacts include: Title, Forename, Surname telephone number etc.
Current Table
+-------------+-----------+------------------------------+
| attributeId | ContactId | AttributeValue |
+-------------+-----------+------------------------------+
| 1 | 5 | Lady |
| 2 | 5 | Elizabeth |
| 3 | 5 | E |
| 4 | 5 | Anson |
| 5 | 5 | |
| 6 | 5 | |
| 7 | 5 | |
| 8 | 5 | |
| 10 | 5 | 0207 72776 |
| 11 | 5 | |
| 12 | 5 | 0207 22996 |
| 13 | 5 | 0207 72761 |
| 14 | 5 | |
| 15 | 5 | |
| 60 | 5 | Lloyds |
| 61 | 5 | |
| 1 | 10 | Mr |
| 2 | 10 | John |
| 3 | 10 | J C |
| 4 | 10 | Beveridge |
| 5 | 10 | Esq QC |
| 6 | 10 | Retired |
| 7 | 10 | |
| 8 | 10 | |
| 10 | 10 | 0207 930 |
| 11 | 10 | |
| 12 | 10 | |
| 13 | 10 | 0207 930 |
| 14 | 10 | |
| 15 | 10 | |
| 60 | 10 | |
| 61 | 10 | |
+-------------+-----------+------------------------------+
However I would like to run a query to create a table that looks like...
New Table
+-----------+----------------------+-------------------------+-----------------------+------------------------+
| ContactId | AttributeValue_Title | AttributeValue_ForeName |AttributeValue_Initial | AttributeValue_Surname |
+-----------+----------------------+-------------------------+-----------------------+------------------------+
| 5 | Lady | Elizabeth | E | Anson |
+-----------+----------------------+-------------------------+-----------------------+------------------------+
| 10 | Mr | John | J C | Beveridge |
+-----------+----------------------+-------------------------+-----------------------+------------------------+
I am sure there is a very simple answer but I have spent hours looking. Can anyone help?
The above is only a small extract of my table, I have 750,000 contacts. In addition I would like the final table to have more columns than I have described above but they will come from different Attributes with the existing table.
Thank you very much in advance.
try this
SELECT ContactId ,
max(CASE when attributeId = 1 then AttributeValue end) as AttributeValue_Title ,
max(CASE when attributeId = 2 then AttributeValue end )as AttributeValue_ForeName ,
max(CASE when attributeId = 3 then AttributeValue end )as AttributeValue_Initial ,
max(CASE when attributeId = 4 then AttributeValue end) as AttributeValue_Surname
from Table1
group by ContactId
DEMO HERE
if you want to make your result more longer for other attributeId then just add a case statment as in the code.
SELECT
t_title.AttributeValue AS title,
t_name.AttributeValue AS name,
...
FROM the_table AS t_title
JOIN the_table AS t_firstname USING(contact_id)
JOIN ...
WHERE
t_title.attributeId = 1 AND
t_firstname.attributeId = 2 AND
...
EAV "model" is an antipattern in most cases. Are you really going to have a variable number of attributes? If yes, then no-SQL solution might be more appropriate than a relational database.