How to use where condition multiple times within one single sql query - php

In my table1 there is date like:
id value
1 500
2 400
3 300
4 200
5 100
I want to set the data above within the show table:
d1 d2 d3 d4 d5
500 400 300 200 100
I am using the following sql query.
"SELECT `value` as d1 from `table1` WHERE `id`=1"
"SELECT `value` as d2 from `table1` WHERE `id`=2"
"SELECT `value` as d3 from `table1` WHERE `id`=3"
"SELECT `value` as d4 from `table1` WHERE `id`=4"
"SELECT `value` as d5 from `table1` WHERE `id`=5"
How will I make all the queries within single sql query?

I would use aggregation:
select max(case when id = 1 then value end) as d1,
max(case when id = 2 then value end) as d2,
max(case when id = 3 then value end) as d3,
max(case when id = 4 then value end) as d4,
max(case when id = 5 then value end) as d5
from t;

If you can work with separate rows, just filter the values:
select id,
value
from t
where id in (1,2,3,4,5);
If you need all the values in a single row, use where to filter all the rows and conditional aggregation to pivot the data:
select max(case when id = 1 then value end) as d1,
max(case when id = 2 then value end) as d2,
max(case when id = 3 then value end) as d3,
max(case when id = 4 then value end) as d4,
max(case when id = 5 then value end) as d5
from t
where id in (1,2,3,4,5);

Related

Combine queries.. PIVOT doesn't work

I have a problem with my queries. I have now 2 queries and that works fine. But when I want to add another query with a PIVOT, it doesn't work. I have tried a lot of things but nothing works..
This is my first two queries
$query = "SET SQL_BIG_SELECTS = 1;";
$query .= "SELECT * FROM datakram, datakram2, datakram3 WHERE datakram.NAME = datakram2.NAME AND datakram2.NAME = datakram3.NAME"
And I want to add a PIVOT for table "datakram4". But I want only the rows where the NAME is equal to the NAME in the others tables. Without the PIVOT it works..
My PIVOT code.
SELECT `name` ,
MAX( CASE WHEN `year` =2017 THEN `income` ELSE 0 END ) AS INCOME_2017,
MAX( CASE WHEN `year` =2017 THEN `expense` ELSE 0 END ) AS EXPENSE_2017,
MAX( CASE WHEN `year` =2016 THEN `income` ELSE 0 END ) AS INCOME_2016,
MAX( CASE WHEN `year` =2016 THEN `expense` ELSE 0 END ) AS EXPENSE_2016
FROM `test_data` GROUP BY `name`
I use multi_query for my php script.
Simply join the queries:
SELECT d4.*
FROM datakram d1
INNER JOIN datakram2 d2
ON d1.`NAME` = d2.`NAME`
INNER JOIN datakram3 d3
ON d2.`NAME` = d3.`NAME`
INNER JOIN
(SELECT `name` ,
MAX(CASE WHEN `year`=2017 THEN `income` ELSE 0 END) AS INCOME_2017,
MAX(CASE WHEN `year`=2017 THEN `expense` ELSE 0 END) AS EXPENSE_2017,
MAX(CASE WHEN `year`=2016 THEN `income` ELSE 0 END) AS INCOME_2016,
MAX(CASE WHEN `year`=2016 THEN `expense` ELSE 0 END) AS EXPENSE_2016
FROM `test_data`
GROUP BY `name`
) d4
ON d3.`NAME` = d4.`name`
A simple way is to filter in the WHERE clause:
SELECT `name` ,
MAX(CASE WHEN `year` = 2017 THEN `income` ELSE 0 END) AS INCOME_2017,
MAX(CASE WHEN `year` = 2017 THEN `expense` ELSE 0 END) AS EXPENSE_2017,
MAX(CASE WHEN `year` = 2016 THEN `income` ELSE 0 END) AS INCOME_2016,
MAX(CASE WHEN `year` = 2016 THEN `expense` ELSE 0 END) AS EXPENSE_2016
FROM `test_data` td
WHERE EXISTS (SELECT 1 FROM FROM datakram d WHERE d.name = td.NAME) AND
EXISTS (SELECT 1 FROM FROM datakram2 d WHERE d.name = td.NAME) AND
EXISTS (SELECT 1 FROM FROM datakram3 d WHERE d.name = td.NAME)
GROUP BY `name` ;

Use Mysql JOIN on column value as column name with another table column name

I want to use a table's column value as column name and use it as JOIN ON value with another table column name. Below is my query.
SELECT nid, sid,
MAX(CASE WHEN cid = 2 THEN data END) first_name,
MAX(CASE WHEN cid = 3 THEN data END) last_name,
MAX(CASE WHEN cid = 4 THEN data END) email,
MAX(CASE WHEN cid = 5 THEN data END) phone_number,
MAX(CASE WHEN cid = 6 THEN data END) transitions,
MAX(CASE WHEN cid = 7 THEN data END) **upload**,
MAX(CASE WHEN cid = 9 THEN data END) coupon_code
FROM wsd AS wsd JOIN files AS fm ON **upload** = fm.fid WHERE nid IN(66,67) GROUP BY sid
As you see the above code, I want to use upload column as the JOIN on another table. Also, is it possible to embed HTML tags on the results?
I tried to get the results using temporary table. Here is my answer.
CREATE TEMPORARY TABLE IF NOT EXISTS my_temp_table SELECT sid, cid, data FROM wsd WHERE cid = 7 AND nid IN (66,67);
SELECT wsd.nid, wsd.sid, fm.filename,
MAX(CASE WHEN wsd.cid = 2 THEN wsd.data END) fn,
MAX(CASE WHEN wsd.cid = 3 THEN wsd.data END) ln,
MAX(CASE WHEN wsd.cid = 4 THEN wsd.data END) email,
MAX(CASE WHEN wsd.cid = 5 THEN wsd.data END) ph,
MAX(CASE WHEN wsd.cid = 6 THEN wsd.data END) tr,
MAX(CASE WHEN wsd.cid = 7 THEN wsd.data END) upr,
MAX(CASE WHEN wsd.cid = 9 THEN wsd.data END) coc
FROM wsd wsd JOIN my_temp_table ON wsd.sid = my_temp_table.sid JOIN files fm ON my_temp_table.data = fm.fid WHERE nid IN(66,67) GROUP BY wsd.sid;
Instead of temporary table, better query as follows:
SELECT wsd.nid, wsd.sid, fm.filename,
MAX(CASE WHEN wsd.cid = 2 THEN wsd.data END) fn,
MAX(CASE WHEN wsd.cid = 3 THEN wsd.data END) ln,
MAX(CASE WHEN wsd.cid = 4 THEN wsd.data END) email,
MAX(CASE WHEN wsd.cid = 5 THEN wsd.data END) ph,
MAX(CASE WHEN wsd.cid = 6 THEN wsd.data END) tr,
MAX(CASE WHEN wsd.cid = 7 THEN wsd.data END) upr,
MAX(CASE WHEN wsd.cid = 9 THEN wsd.data END) coc
FROM wsd wsd LEFT JOIN files fm ON wsd.data = fm.fid WHERE nid IN(66,67) GROUP BY wsd.sid;

MySQL Cross table query join

I want to create an output table that is a combination of the two input tables, as shown below. Table 1 and table 2 are MySQL tables. Output is the desired table view.
Please hlep me to billd this output from table1 and table2 (mysql)
You can use conditional aggregation with UNION ALL for this:
SELECT t1.acc_no, t1.name,
SUM(CASE WHEN t2.`date` = '2016-06-01' THEN amount END) AS '2016-06-01',
SUM(CASE WHEN t2.`date` = '2016-06-02' THEN amount END) AS '2016-06-02',
SUM(CASE WHEN t2.`date` = '2016-06-03' THEN amount END) AS '2016-06-03',
SUM(amount) AS 'Total'
FROM table1 AS t1
LEFT JOIN table2 AS t2 ON t1.acc_no = t2.acc_no
UNION ALL
SELECT 'Total', null,
SUM(CASE WHEN t2.`date` = '2016-06-01' THEN amount END) AS '2016-06-01',
SUM(CASE WHEN t2.`date` = '2016-06-02' THEN amount END) AS '2016-06-02',
SUM(CASE WHEN t2.`date` = '2016-06-03' THEN amount END) AS '2016-06-03',
SUM(amount) AS 'Total'
FROM table2

use parent query field in nested query?

I am using this code to get the data of a team
SELECT `in_standings`.*, `in_teams`.`name` as team_name, `in_teams`.`logo` as team_logo, `in_teams`.`shortcode` as team_shortcode, lr.*, in_teams.id as team_id
FROM `in_standings` daa
LEFT JOIN `in_teams` ON `in_standings`.`team_id`= `in_teams`.`id`
left join
(SELECT team1_id,daa.team_id as teamID,
MAX(CASE WHEN rn = 1 THEN winning_team END) AS result1,
MAX(CASE WHEN rn = 2 THEN winning_team END) AS result2,
MAX(CASE WHEN rn = 3 THEN winning_team END) AS result3,
MAX(CASE WHEN rn = 4 THEN winning_team END) AS result4,
MAX(CASE WHEN rn = 5 THEN winning_team END) AS result5
FROM (
SELECT team1_id, winning_team,
#row_number := #row_number + 1 AS rn
FROM in_games
CROSS JOIN (SELECT #row_number := 0) AS vars
WHERE team1_id = teamID <!-- here is problem -->
) AS t) as lr on lr.team1_id=in_standings.team_id
WHERE `in_standings`.`division_id` = '21'
ORDER BY `in_standings`.`points` DESC
My sub query where i comment is not getting the team_id by which i want to set the result of last 5 matchs in a same row.
Could anyone solve where is the problem?

Proper mysql code to alter table to horizontal

I am trying to make the table below look like so
Current Query:
SELECT m.typeID, t.typeName, m.quantity, t.description
FROM invTypeMaterials AS m
LEFT JOIN invTypes AS t
ON m.materialTypeID = t.typeID
LEFT JOIN invTypes
ON m.TypeID = t.typeID
WHERE m.typeID = 10039
Current Result
typeID typeName quantity description
10039 Tritanium 71 The main building block in space structures. A ver...
10039 Pyerite 24 A soft crystal-like mineral with a very distinguis...
10039 Mexallon 1 Very flexible metallic mineral, dull to bright sil...
What I'm look for is for it to show up like this
typeID Tritanium Pyerite mexallon description
10039 71 24 1 Doesn't matter
I have tried combinations of left join, outer join, inner join etc and cant make it work. I also tried group_concat which I think is what I need but cant get the columns right
Thanks
EDIT: I have the column looking like I want by using the following:
SELECT m1.typeID,
SUM(CASE WHEN m1.materialTypeID = 34 THEN m1.quantity ELSE 0 END) AS Tritanium,
SUM(CASE WHEN m1.materialTypeID = 35 THEN m1.quantity ELSE 0 END) AS Pyerite,
SUM(CASE WHEN m1.materialTypeID = 36 THEN m1.quantity ELSE 0 END) AS Mexallon,
SUM(CASE WHEN m1.materialTypeID = 37 THEN m1.quantity ELSE 0 END) AS Isogen,
SUM(CASE WHEN m1.materialTypeID = 38 THEN m1.quantity ELSE 0 END) AS Nocxium,
SUM(CASE WHEN m1.materialTypeID = 39 THEN m1.quantity ELSE 0 END) AS Zydrine,
SUM(CASE WHEN m1.materialTypeID = 40 THEN m1.quantity ELSE 0 END) AS Megacyte,
SUM(CASE WHEN m1.materialTypeID = 11399 THEN m1.quantity ELSE 0 END) AS Morphite
FROM invTypeMaterials AS m1
WHERE m1.typeID = 10039
RESULT:
typeID Tritanium Pyerite Mexallon Isogen Nocxium Zydrine Megacyte Morphite
10039 71 24 1 0 0 0 0 0
When i take out the "WHERE" clause and try to pull them all it shows up like this:
typeID Tritanium Pyerite Mexallon Isogen Nocxium Zydrine Megacyte Morphite
18 13989190531 1301687143 261706229 54807927 14277967 4262960 1540121 40701
Any ideas?
UPDATE 2: I got it, the GROUP BY did the trick
Assuming the typeName values are always the same you can use subqueries in the select section of your query to get the values you want.
$sql = "SELECT m.typeID,
(SELECT m.quantity FROM invTypes AS t WHERE m.materialTypeID = t.typeID AND t.typeName = 'Tritanium') as Tritanium,
(SELECT m.quantity FROM invTypes AS t WHERE m.materialTypeID = t.typeID AND t.typeName = 'Pyerite') as Pyerite,
(SELECT m.quantity FROM invTypes AS t WHERE m.materialTypeID = t.typeID AND t.typeName = 'Mexallon') as Mexallon
FROM invTypeMaterials AS m
WHERE m.typeID = 10039";
I don't think there's any way in SQL to generate dynamic columns. If you can write dynamic SQL, then yes but in pure SQL, I don't think so.
If you're willing to hard-code the columns then you can do
SELECT
m.TypeID,
MAX(CASE
WHEN t.typeName = 'Tritanium' THEN m.quantity
ELSE 0
END) Tritanium,
MAX(CASE
WHEN t.typeName = 'Pyerite' THEN m.quantity
ELSE 0
END) Pyerite,
MAX(CASE
WHEN t.typeName = 'Mexallon' THEN m.quantity
ELSE 0
END) Mexallon
FROM
[your FROM clause]
WHERE
[your WHERE clause]
GROUP BY
m.TypeID;
We have pivot clause in oracle to do similar kind of functionality.
It helps in writing cross tabulation query i.e., we can aggregate results and rotate rows into columns.
tried the below with some sample data:
create table types(typeid number,typename varchar2(20),
quantity number);
insert into types values(1,'xx',11);
insert into types values(1,'yy',121);
insert into types values(1,'zz',113);
delete from types where typeid in(2,3);
select *from
(select typeid,typename,quantity from types)
pivot
(
sum(quantity)
for typename in('xx','yy','zz')
)
typeid xx yy zz
1 11 121 113

Categories