how to combine 2 mysql rows and get one row - mysql

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'

Related

JOIN 2 columns with same name but from different tables with condition from another column

I need to get every value from 2 columns with same names in different tables. All of that with a match condition from another column
//TABLE1
city | code |
here | 123 |
there | 567 |
another| 498 |
//TABLE2
city | code |
here | 813 |
there | 379 |
another| 111 |
Expected result from city "there"
| 567 |
| 379 |
I tried many possibilities with JOIN and UNION but i can find the right way
I would like to suggest to use anyone from the following query and you can see the result in this link :
[http://sqlfiddle.com/#!9/ded8d6/13][1]
1.
SELECT code FROM table1 WHERE city = 'there'
UNION
SELECT code FROM table2 WHERE city = 'there'
2.
SELECT code FROM table1 WHERE city = 'there'
UNION ALL
SELECT code FROM table2 WHERE city = 'there'
The only difference between Union and Union All is that Union extracts the rows that are being specified in the query while Union All extracts all the rows including the duplicates (repeated values) from both the queries.
I you need to vertically join table, you can use UNION
SELECT code FROM table1 WHERE city = 'there'
UNION
SELECT code FROM table2 WHERE city = 'there'
If ou have more than a small amount of taböe, you need to aggregate with cahe or if clause
I suggest you these 2 requests, you can see the result in this link:
http://sqlfiddle.com/#!9/ded8d6/9
(SELECT code FROM TABLE1 WHERE city = 'there')
UNION
(SELECT code FROM TABLE2 WHERE city = 'there');
SELECT code FROM
(
(SELECT * FROM TABLE1)
UNION
(SELECT * FROM TABLE2)
) T
WHERE T.city = 'there';
If you set a variable for the city, then it doesn't need to be hardcoded twice in the union query.
set #city = 'there';
select code
from TABLE1
where city = #city
union
select code
from TABLE2
where city = #city
order by code
code
379
567
Demo on db<>fiddle here

Getting values from 2 tables with different conditions for both tables

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'
);

SQL mapping from 1 table

I have 2 mysql tables that look like this:
Tabel 1 :
ID NAME
1 car1
2 car2
3 car3
4 car4
Tabel 2 :
car_id liter
2 100
2 300
3 400
1 500
3 600
I want to output something like:
Tabel 3:
car_id liters
car2 100
car2 300
car3 400
car1 500
car3 600
I try to write something like:
SELECT tabel2.car_id
, tabel1.ID
FROM tabel2
, tabel2
INNER
JOIN tabel3
ON tabel2.car_id = tabel1.ID;
I know this is kinda a newbie question, but I'm new to SQL.
Simply join the two tables:
select t1.name, t2.liter
from table1 t1
join table2 t2 on t1.id = t2.car_id
order by t2.liter
select t1.name, t2.liter from table1 t1 ,table2 t2 where t1.id = t2.car_id
order by t2.liter;
You can try this to get the desired result:
select t1.NAME, t2.liter
from table1 t1
INNER JOIN table t2 on t1.id=t2.car_id
ORDER BY t2.liter DESC
Try this:
select t1.name, t2.liter
from table1 t1 ,
table2 t2
here t1.id = t2.car_id ;

Fetch data from Table 1 column's count in Table 2

Table 1 :
ID City State
1 NewYork NY
2 Oklahama OK
3 california CA
4 new jersey NJ
5 Las Vegas LA
Table 2 :
ID City
1 NewYork
2 NewYork
3 NewYork
4 Oklahama
5 Oklahama
NewYork is 3 times and Oklahama is 2 times in table 2.
So I want to fetch City List from Table 1 which Cities are used less then 5 times in Table 2
So what will exact query in Mysql?
I am using below code :
select *
from Table1
where Table1.city in
(select Table2 .city,count(*)
from Table2
having count(*) < 5
group by Table2.city )
You can use a in clause with a subselect
select * from table1
where city in (select city from table2 having count(*) < 5 group by city)
In your code you have a space between the Table2 and .city
select *
from Table1 where Table1.city in (select
Table2 .city,count(*) from Table2 having count(*) < 5 group by Table2.city )
must be
select *
from Table1
where Table1.city in (select Table2.city
from Table2
group by Table2.city
having count(*) < 5 )
no space between tablename and column ..
you can seee http://sqlfiddle.com/#!9/556cb6/10
How about
select Table1.city
from Table1 inner join Table2 on Table1.city = Table2.city
group by Table1.city
having count(Table2.city) < 5
SQLFiddle

SQL "group by" use priority

I have a table where I can have multiple names for a given id like this:
a_table (id int, name varchar(100), priority int);
I need a query that will search on names but make sure it will return only 1 name for each id, and that name will be the one with the higher priority.
e.g. if my data are
1, AaaB, 2
1, AbbB, 1
1, AccB, 0
2, foo, 0
3, AddC, 0
I want my query for "A%" to return:
1, AaaB
3, AddC
I was thinking something like:
select * from a_table where name like 'A%' group by id;
But this will not guarantee that the value with the higher priority will be selected.
Any ideas?
I believe you want what the MySQL documentation calls the rows holding the group-wise maximum of a certain column:
For the task "For each article, find the dealer or dealers with the most expensive price":
SELECT article, dealer, price
FROM shop s1
WHERE price=(SELECT MAX(s2.price)
FROM shop s2
WHERE s1.article = s2.article)
ORDER BY article;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | C | 1.69 |
| 0004 | D | 19.95 |
+---------+--------+-------+
You have to first get the highest priority per id and then filter on the names:
select t2.id, t2.name, t2.price
from (
select id, max(priority)
from a_table
group by id
) t1,
a_table t2
where t1.id = t2.id
and t1.priority = t2.priority
and t2.name like 'A%'
Taking #niktrs's valid suggestion, this is the same above query using the standard JOIN instead of where for joining tables. This is more preferred over the previous one
select t2.id, t2.name, t2.price
from (
select id, max(priority)
from a_table
group by id
) t1 JOIN a_table t2 ON t1.id = t2.id
and t1.priority = t2.priority
and t2.name like 'A%'
select *
from a_table t
join (
select max(Priority) MaxPriority, Name
from a_table a
where name like 'A%'
group by Name
)x where x.MaxPriority=a.Priority and x.Name=t.Name
On the basis the first column in the data example is the "Priority".
This is just the SQL linked from Alvaro's answer though.
select id, name
from a_table at
where
name like 'A%' and
priority = (
select max(priority)
from a_table
where (id = at.id) and (name like 'A%')
)
would this work out,
select distinct id,first_value(name)over(partition by id order by name)
from demo_tab t
where t.name like 'A%'
Sorry pratik,
it must have been
select distinct id,first_value(name)over(partition by id order by priority desc)
from demo_tab where name like 'A%'