Getting values from 2 tables with different conditions for both tables - mysql

Table 1
ID
FirstName
LastNmae
city
Group
code
11
john
smith
abc
E
P
21
don
davis
def
E
P
3
vee
miller
ghi
Q
P
6
vee
miller
ghi
Q
P
Table 2
ID
FirstName
LastNmae
city
Status
EmpName
Phone
11
john
smith
abc
U
Company 1
123
21
don
davis
def
P
Company 2
456
3
vee
miller
ghi
C
Company 3
789
4
jim
jones
xyz
P
comapany4
001
I have 2 tables mentioned above. I need an output from both table under these conditions
For table 1 condition is:
Group='E' AND code='P'
For table 2 condition is : Status = 'U' OR Status = 'P'
For output required columns are:
ID, FirstName, LastName, City, EmpName, Phone
I cannot use UNION because number of columns mismatch.
Desired Output:
ID
FirstName
LastNmae
city
EmpName
Phone
11
john
smith
abc
Company 1
123
21
don
davis
def
Company 2
456
4
jim
jones
xyz
comapany4
001
How can i get desired output. With UNION i can't get "EmpName" and "Phone" column. Is there anyway to use JOIN to get desired output.

I think you still need UNION. Try this query:
SELECT ID, FirstName, LastName, city, EmpName, Phone
FROM table2
WHERE Status IN ('U', 'P')
UNION
SELECT t1.ID, t1.FirstName, t1.LastName, t2.city, t2.EmpName, t2.Phone
FROM table1 AS t1
LEFT JOIN table2 AS t2 ON t1.ID = t2.ID
WHERE t1.Group = 'E' AND t1.code = 'P'
;

First, your results are all coming from table2, so this returns the results specified in the question:
select t2.ID, t2.FirstName, t2.LastName, t2.city, t2.EmpName, t2.Phone
from table2 t2
where t2.status in ('U', 'P');
I think you also want a matching condition on table1 (which is not needed for your example). Based on your description:
select t2.ID, t2.FirstName, t2.LastName, t2.city, t2.EmpName, t2.Phone
from table2 t2
where t2.status in ('U', 'P') or
exists (select 1
from table1 t1
where t1.id = t2.id and
t1.Group = 'E' and t1.code = 'P'
);

Related

Appending row values of a table with columns of another table

I have a table T1 with standard columns like name, email, phone_number. The custom fields are store in custom_fields table as key, value.
I need to have key name of custom_fields as column name on actual table. How can I achieve this
Table T1
id name email
1 John john#example.com
2 Sam same#mydomain.com
custom_fields
id T1_id key value
1 1 Age 32
2 1 Job Title CEO
3 2 Age 40
4 2 Car Owned Ford EcoSport
Required
T1_id name email age job_title car_owned
1 John john#example.com 32 CEO Not specified
2 Sam same#mydomain.com 40 Not specified Ford EcoSport
You should use the table custom_fields two time one for job and one for car
select t1.id, t1.name, t1.email, t2.value, t3.value
from t1
inner join custom_fields t2 on t1.id = t2.t1_id and t2.key = 'Job Title'
inner join custom_fields t3 on t1.id = t3.t1_id and t3.key = 'Car Owned'

how to combine 2 mysql rows and get one row

I have a table report_card and,
Sample data as below.
report_id orig_id test_id name address
------------------------------------------------------
1 JH06E IN2001 xyz delhi
2 HL789 IN2001 abc mumbai
3 ZPYNR IN2002 pqr mumbai
4 5R4HJ IN2002 mno delhi
and I want result like to get all the rows of mumbai but address is delhi instead of mumbai ?
here I have id 1 and 2 have same test_id and 3 and 4 have also. Output like below,
report_id orig_id test_id name address
------------------------------------------------------
2 HL789 IN2001 abc delhi
3 ZPYNR IN2002 pqr delhi
A self join like:
SELECT t1.report_id, t1.orig_id, t1.test_id, t1.name, t2.address
FROM report_card t1
JOIN report_card t2
ON t1.test_id = t2.test_id
WHERE
t1.address = 'mumbai' AND
t2.address = 'delhi'
What #danblack have written is correct, but to select the orig_id of mumbai you need to write your select like following.
SELECT t1.report_id,
t2.orig_id,
t1.test_id,
t1.name,
t1.address
FROM mytable t1
INNER JOIN mytable t2
ON t1.test_id = t2.test_id
WHERE t1.address = 'delhi'
AND t2.address = 'mumbai'
Edit :
sorry here i change my result on question, can i get all the rows of
mumbai but address is delhi instead of mumbai
You can also do it using subquery like following.
SELECT t1.report_id,
t1.orig_id,
t1.test_id,
t1.name,
(SELECT address
FROM mytable m
WHERE m.test_id = t1.test_id
AND m.address = 'delhi'
LIMIT 1)AS address
FROM mytable t1
WHERE t1.address = 'mumbai'
But looking at your data and the expected result, you just need like following
SELECT t1.report_id,
t1.orig_id,
t1.test_id,
t1.name,
'delhi' address
FROM mytable t1
WHERE t1.address = 'mumbai'

Using a nested query to get details of two tables

TABLE 1 TABLE 2
id name mob id course mark
1 joe 0000 1 English 77
2 john 0000 2 maths 89
I need to show the name of the person from table 1 who has the MAX(grade) in table 2 using a nested query.
SELECT t1.name
FROM t1
WHERE t1.id = t2.id = (
SELECT id
FROM t2
WHERE mark =
(
SELECT MAX(mark)
FROM t2
)
);
Well, this satisfies the brief ;-):
SELECT a.*
FROM table_a a
JOIN (SELECT * FROM table_b) b
ON b.id = a.id
ORDER
BY mark DESC
LIMIT 1;

How to union one column only from multiple tables?

I am finding it hard to frame the question so I will show what I am trying to do.
Table1 (ID, Name, Score)
1 Jane 10
2 Jack 15
3 Jill 12
4 Jane 10
Table2 (ID, Name, Score)
1 John 11
2 Jill 14
3 Jack 16
4 Jake 15
The result I would like is
Result (Name, Table1.Score, Table2.Score)
Jane 20 NULL
Jack 15 16
Jill 12 14
John NULL 11
Jake NULL 15
Can name become the primary key? Note that Jane occurs twice in table 1(could occur in table 2 as well) and I want to sum the score and make the name unique in that given table.
Here's one way to do it with UNION and SUM:
SELECT T.Name, Sum(T.Score), Sum(T2.Score) Score2
FROM (SELECT Name, SUM(Score) score FROM Table1 GROUP BY Name) t
LEFT JOIN (SELECT Name, SUM(Score) score FROM Table2 GROUP BY Name) t2
ON t.Name = t2.Name
GROUP BY T.Name
UNION
SELECT T.Name, Sum(T2.Score), Sum(T.Score) Score2
FROM (SELECT Name, SUM(Score) score FROM Table2 GROUP BY Name) t
LEFT JOIN (SELECT Name, SUM(Score) score FROM Table1 GROUP BY Name) t2
ON t.Name = t2.Name
GROUP BY T.Name
And here is the SQL Fiddle.
Good luck.
try this
select t1.name , sum(t1.score) as score1 ,sum(t2.score2) as score2 from Table1 as t1
left JOIN
(select name , score as score2 from Table2 )t2
on t1.name = t2.name
group by name
union all
select t2.name , sum(t1.score1) as score1, sum(t2.score) as score2 from Table2 as t2
left JOIN
(select name , score as score1 from Table1 )t1
on t2.name = t1.name
where score1 is null
group by name
---- if you want use the order add this "order by name" here in the end
and this will output :
NAME | SCORE1 |SCORE2
Jack | 15 | 16
Jane | 20 |(null)
Jill | 12 | 14
Jake | (null) | 15
John | (null) | 11
HERE SQLFIDDLE DEMO
Well, if MySQL had full outer join, you would use that. Here is an alternative method:
select id, name, MAX(score1) as score1, MAX(score2) as score2
from ((select id, name, score as score1, NULL as score2
from table1
) union all
(select id, name, null as score1, score as score2
from table1
)
) t
group by id, name
The idea is to get all information needed from each table, with the score in different columns, by using the union all and splitting the score into two separate columns.
The final group by brings them together with one row per id.
Try this:
select
name = Table1.name,
sum(Table1.score) as score1,
sum(Table2.Score) as score2
from Table1
full outer join
Table2
ON
Table2.name = Table1.name
where Table1.name is not null
group by table1.name
UNION
select
name = Table2.name,
sum(Table1.score) as score1,
sum(Table2.Score) as score2
from Table1
full outer join
Table2
ON
Table2.name = Table1.name
where Table2.name is not null
group by table2.name
order by score1 desc, score2

MySQL statement: spliting rows values into new columns

Using MySQL I need to return new first/last name columns for each user in table1.
**Table1**
uid
1
2
**Table2**
id fid uid field_value
1 1 1 Joe
2 2 1 Blow
3 1 2 Joe
4 2 2 Blogs
**Result**
uid first_name last_name
1 Joe Blow
2 Joe Blogs
The solution is simple,
select t1.uid, t21.field_value first_name, t22.field_value last_name
from table1 t1, table2 t21, table2 t22
where t1.uid=t21.uid and t1.uid=t22.uid and t21.fid=1 and t22.fid=2
This should do it (not tested):
select a.uid, a.field_value first_name, b.field_value last_name
from table2 a inner join table2 b on a.uid = b.uid
where a.fid = 1 and b.fid = 2
assuming you have columns first_name, and last_name in table2:
select uid, first_name, last_name from table1 left join table2 on table1.uid=table2.uid