sql to extract same columns from 3 different tables - mysql

Hi i have 3 tables i want to extract same columns from 3 tables is this better way to write Select query.
select * from
(
select col1,
select id1 from testid1 where name=pnrtable1.name,
col3 from table1
union all
select coltab1,
select newid2 from testid2 where name=pnrtable2.name,
coltab3 from table2
union all
select namecol1,
select id3 from testid3 where name=pnrtable3.name,
namecol3 from table3
)

Not sure but I think you are after something like this....
select * from
(
select t1.col1, t2.id1 , t1.col3 from table1 t1
INNER JOIN testid1 t2 ON t1.name = t2.name
union all
select t1.coltab1, t2.newid2, t1.coltab3 from testid2 t1
INNER JOIN table2 t2 ON t1.name=t2.name
union all
select t1.namecol1, t2.id3, t1.namecol3 from testid3 t1
INNER JOIN table3 t2 ON t1.name=t2.name
) A

Related

Inner join one to many unique_id between two table

one table t1 has one unique id u1
another table t2 has three unique_id u2a, u2b, u2c
How to make an inner join between two tables so that
u1 of table t1 join either u2a, u2b or u2c of table t2.
I don't think this is a best practice but try:
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2a
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2b
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2c;
Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/116
If you want the name in order you can add order by name desc; in the end of the query.
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2a
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2b
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2c
order by name desc;

How to select the id with max value that in another table

Here are 2 tables.
Table 1
id value
1 3
2 2
3 3
4 1
5 4
6 3
Table 2
id
1
3
4
How do I get the ids that are in Table 2 which have the max value in Table 1?
Output:
id
1
3
I already tried the following to get the max value, but I cannot figure out how to use it in a single query to get the matching rows. Because I think I need to select from the same table I just inner joined.
select max(table1.value)
from table2
inner join table1 on table1.id = table2.id;
Here is one method:
select t2.id
from (select t2.*, rank() over (order by value desc) as seqnum
from table2 t2 join
table1 t1
on t2.id = t1.id
) t
where seqnum = 1;
Or, an alternative that puts all the ids on one row:
select group_concat(t2.id) as ids
from table2 t2 join
table1 t1
on t2.id = t1.id
group by t1.value
order by t1.value desc
limit 1;
You have a couple of options available without using window functions:
You can use a WHERE clause to select only id values that have a value equal to the MAX(value) from your query and an id that is in Table2:
SELECT t1.id
FROM Table1 t1
WHERE value = (
SELECT MAX(t1.value)
FROM Table2 t2
JOIN Table1 t1 ON t1.id = t2.id
)
AND id IN (SELECT id FROM Table2)
You can JOIN your query to Table1 and Table2 again, matching the value in Table1 and the id in Table2:
SELECT t1.id
FROM (
SELECT MAX(t1.value) AS max_value
FROM Table2 t2
JOIN Table1 t1 ON t1.id = t2.id
) t
JOIN Table1 t1 ON t1.value = t.max_value
JOIN Table2 t2 ON t2.id = t1.id
In both cases the output is
id
1
3
Demo on SQLFiddle
Too low to comment but from the SQL statement you gave, you just need to add the tableid in your select parameters.
select table2.id, max(table1.value)
from table2
inner join table1 on table1.id = table2.id;

Mysql: ignore 2 distinct columns from a table

I have 2 tables that are something like this:
Table1:
animal name color
cat bob red
dog bill blue
Table2:
animal name
cat bob
fish joe
I want to query all the data in table 1 except if they have the unique identity from table 2, so I would get dog bill blue, and not the cat, since it appears in table 2.
I did:
select t.animal, t.name, t.color
from Table1 as t, Table2 as t2
where NOT(t.animal = t2.animal AND
t.name = t2.name)
group by t.animal, t.name
Is there a more efficient way of doing this?
select t.animal, t.name, t.color
from Table1 as t
LEFT JOIN Table2 as t2 ON t.animal = t2.animal and t.name = t2.name
where table2.name is null
group by t.animal, t.name
A simple where not exists clause should do the trick:
select t1.animal, t1.name, t1.color
from Table1 t1
where not exists
(
select 1
from Table2 t2
where t2.animal = t1.animal
and t2.name = t1.name
)
You can also use the anti-join pattern:
select t1.animal, t1.name, t1.color
from Table1 t1
left outer join Table2 t2 on t1.animal = t2.animal and t1.name = t2.name
where t2.animal is null
SELECT * FROM table_one t1
WHERE t1.animal NOT IN
(SELECT animal FROM table_two)

Condition based select and join queries

Query 1:
SELECT if(COUNT(0),1,0) as 'IsPresent'
FROM table1
WHERE Id=1500;
Query2:
If IsPresent is 1, then
select t2.mark,t2.age from table2 t2,table1 t1
where t1.ID=t2.ID order by t1.ID;
If IsPresent is 0, then
select mark,age from table2;
ie. if entry is present in a table, i need to join else i don't need to join.
Is there any way we can achieve this with a single mysql select query?
I think you can union the two different query cases which would look like:
SELECT T2.MARK, T2.AGE
FROM TABLE1 T1, TABLE2 T2
WHERE
T1.ID=T2.ID AND
T1.ID=1500
UNION
SELECT MARK, AGE
FROM TABLE1
WHERE
NOT ID=1500
SELECT t2.mark, t2.age
FROM table2 t2
JOIN table1 t1
ON t1.id = t2.id
WHERE EXISTS
( SELECT *
FROM table1
WHERE id=1500
)
UNION ALL
SELECT t2.mark, t2.age
FROM table2 t2
WHERE NOT EXISTS
( SELECT *
FROM table1
WHERE id=1500
)
which can be simplified to:
SELECT t2.mark, t2.age
FROM table2 t2
LEFT JOIN table1 t1
ON t1.id = t2.id
AND EXISTS
( SELECT *
FROM table1
WHERE id=1500
)

How to retrieve the not common values from 2 tables?

I have the 2 following tables t1, t2 with values,
t1 t2
1 4
2 2
3 3
Now I want to output
1
4
How can I get this output in select query ?
This will get you each item from t1 that is not present in t2, and each item in t2 that is not present in t1:
select t1.id from t1
left join t2 on t2.id = t1.id
where t2.id is null
union all
select t2.id from t2
left join t1 on t1.id = t2.id
where t1.id is null
(I have assumed that the field name in each table is named id just for the sake of being able to write a query against the tables.)
Another way would be:
select coalesce(t1.id, t2.id)
from t1
full outer join t2 on t2.id = t1.id
where t1.id is null or t2.id is null
Another way. Just COUNT them.
This works if the values are unique per table
SELECT
CombinedValue
FROM
(
SELECT t1 AS CombinedValue FROM t1
UNION ALL
SELECT t2 FROM t2
) foo
GROUP BY
CombinedValue
HAVING
COUNT(*) = 1
If not unique per table
SELECT
CombinedValue
FROM
(
SELECT DISTINCT t1 AS CombinedValue FROM t1
UNION ALL
SELECT DISTINCT t2 FROM t2
) foo
GROUP BY
CombinedValue
HAVING
COUNT(*) = 1
you can use Joins in MySql to proceed and to obtain result.
this will help you
http://www.techrepublic.com/article/sql-basics-query-multiple-tables/1050307