MYSQL want to compare two table and select data - mysql

I have two tables.
table 1
1
2
3
4
table 2
1
2
3
i want to select data from tables
table 1 have all data.
but table 2 may have all data or not.
so
if table 1 data exits in table two data select table 2 data otherwise select table one data.
if table 1 data not exit table 2 select table 1 data.
how can do that?

You need OUTER JOIN:
SELECT t1.ID,
COALESCE(t2.col1, t1.col1) AS col1, -- prefer data from table_2 if exists
COALESCE(t2.col2, t1.col2) AS col2,
-- ...
FROM table_1 t1 -- "table 1 have all data"
LEFT JOIN table_2 t2 -- "table 2 may have all data or not"
ON t1.ID = t2.ID;

Try FULL OUTER JOIN
SELECT t2.col1, t1.col1
FROM
table_1 t1 FULL OUTER JOIN table_2 t2
ON t2.col1 = t1.col1
Or, if full joins are not supported by your database, try emulating them.

Related

Single SQL statement based on the existence of foreign keys

I have two tables in a database
Table1
T1ID
T1Value
1
T1V1
2
T1V2
3
T1V3
4
T1V4
Table 2
T2ID
T1FK
T2Value
1
2
T2V1
2
3
T2V2
Please note that Table2 has a foreign key, that references T1ID in Table1. Also note that not all entries in Table1 have corresponding entries in Table2.
Now, I would like to use a SINGLE SQL statement to do both of the two following
If there are corresponding entries in Table2 related to Table1, then run the following statement
SELECT t1.*, t2.T2Value FROM Table1 AS t1 INNER JOIN Table2 AS t2 ON t1.T1ID = t2.T1FK WHERE t1.T1ID = 2
If there aren't any corresponding entries in Table2 related to Table1, then run the following statement (notice that NULL is returned instaed of t2.T2Value)
SELECT t1.*, NULL FROM Table1 AS t1 WHERE t1.T1ID = 4
(Please note that t1.T1ID is supplied by the program, so this value will change. I am just showing you what I need)
How do I combine both the above statements into a single statement?
Thanks.
A simple LEFT JOIN will do what you want. For example:
select t1.*, t2.t2value
from table1 t1
left join table2 t2 on t1.t1id = t2.t1fk
where t1.t1id = <parameter>
For example:
If the parameter is 2, then a related row is found and the query returns the value of it (t2.t2value).
If the parameter is 4, then no related row is found and the query returns NULL in its place.

Sql merging two text columns into single column

I have two tables t1 & t2.
table t1
user_id tags
1 a,b,c
2 b,c
table t2
user_id tags
1 d,c
2 c,d
I want to merge this into table t1. How can i do this.
table t1
user_id tags
1 a,b,c,d
2 b,c,d
I am new in sql.
Try this, although having more than one value in one field is not considered good practice and I couldn't recommend it.
SELECT t1.user_id, CONCAT(t1.tags, ', ', t2.tags) AS Tags
FROM table1 AS t1 INNER JOIN table2 AS t2 on t1.user_id = t2.user_id
This won't show unique values but will come close to what you request, which is hampered by your data format.
**
SQL Fiddle
**

mysql subqueries or join

I have 5 tables in my Mysql database with t1 with field name like data,id,cool and 4 other tables with id and data say t2,t3,t4,t5.
I have used join and sub queries but result i am getting is empty set its not mandatory that all the fields in the t2,t3,t4,t5 tables are populated .t4,t5,t3,t2 can be empty i am using where clause on data if data column of t1 = t2 = t3 = t4 = t5 if it finds any matching data in any of the table it prints the cool from t1 and id and data from found table if any and if founds the data match in all the table it prints all the table stats like
cool data.t1 id.t1
cool data.t2 id.t2
and so on
table t1
cool data id
0 xyz 1
table t2
data id
xyz 5
table t3
data id
xyz 4
table t4
data id
xyz 3
table t5
data id
xyz 2
desired output
cool data id
0 xyz 1
0 xyz 4
0 xyz 3
0 xyz 5
0 xyz 2
if any of the table is empty say t5 and t4 than output should be
cool data id
0 xyz 1
0 xyz 4
0 xyz 3
What i understand from the question is you need to get data from table t1 for all other tables for all matching data and id column in respective table.
A simple way to achieve is to join each table with t1 seperately and then use union all to combine all results.
SELECT t1.cool, t2.data, t2.id
FROM t1 INNER JOIN t2 ON (t1.id=t2.id) AND (t1.data=t2.data)
UNION ALL
SELECT t1.cool, t3.data, t3.id
FROM t1 INNER JOIN t3 ON (t1.id=t3.id) AND (t1.data=t3.data)
UNION ALL
SELECT t1.cool, t4.data, t4.id
FROM t1 INNER JOIN t4 ON (t1.id=t4.id) AND (t1.data=t4.data)
UNION ALL
SELECT t1.cool, t5.data, t5.id
FROM t1 INNER JOIN t5 ON (t1.id=t5.id) AND (t1.data=t5.data)
as from your example, it is quite possible that there can be duplicate records To filter them out use distinct like this -
SELECT DISTINCT cool, data, id
FROM
(
-- above query here
)

MySQL: joining tables through a series of relations - reducing duplicate table reads

I'm reading an article about join in MySQL, but I don't understand why Joining tables through a series of relations has the advantage of reducing duplicate table reads.
Can someone explain with examples? Thanks.
Quoted text below:
Stated visually, these two ways of joining tables looks like:
Series of Relations: table 1 -> 2 -> 3
Common Relation: table 1 -> 2, (1) -> 3
Either way, MySQL reads table 1, then 2, then 3—its "single-sweep
multi-join method." The "(1)" in the common relation join means that
when table 3 is read, rows in it are found using its relation to table
1, not table 2 as in the series of relations join. Joining tables
through a series of relations has the advantage of reducing duplicate
table reads if the first table is the most restrictive, the second
table less restrictive, the third table even less restrictive, etc.
Text from hackmysql.com/case5
I believe it is simply referencing to the following two (unrealistic) examples, although I could be wrong.
Note the way in which table 3 is joined in each.
Table 1 -> 2 -> 3
SELECT t1.name, t2.phoneNo, t3.address
FROM table1 t1
JOIN table2 t2
ON t2.id = t1.id
JOIN table3 t3
ON t3.id = t2.id
WHERE t1.id = 123
Table 1 -> 2, (1) -> 3
SELECT t1.name, t2.phoneNo, t3.address
FROM table1 t1
JOIN table2 t2
ON t2.id = t1.id
JOIN table3 t3
ON t3.id = t1.id
WHERE t1.id = 123
Regards,
Chris

Mysql Query optimization

Below is my Table Structure
Table 1
id
name
Table 2
id
table1_id
I want the rows from table 1 which have no reference value in table 2.
Example data:
Table 1
id name
1 demo
2 demo2
3 demo3
Table 2
id table1_id
1 1
2 1
3 1
4 3
5 3
So there is no value in table 2 with table1_id 2. I want id 2 from Table 1.
Below id the query i have tried:
SELECT l.id FROM Table1 l WHERE l.id NOT IN (SELECT DISTINCT(r.id) FROM table2 r);
This is returning a proper result but its taking more than 2 minutes to process.
In table 1 i have 4000 rows and in table 2 I have 40000 rows.
Any optimisation to above query or any alternative solution?
SELECT * FROM table1 LEFT JOIN table2
ON table1.id=table2.table1_id
WHERE table2.table1_id IS NULL
Have an index for Table1.id and Table2.table1_id, then try the following query:
SELECT Table1.id FROM Table1
WHERE Table1.id NOT IN (SELECT Table2.id FROM Table2 group by Table2.table1_id);
What you are trying to acheive is to find orphan records right?
A join that shows all the records from the first(the left) table and the matching values form the other or nulls for no matches is called a left join. I think a left join will do the same job but it is not going to be any faster. Joins are in general slower.
I found a place where it is all well explained - http://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/
It does not hurt to try with a join though, and tell us were your results the same as expected.
select t1.id from table1 as t1
left outer join table2 as t2
on t2.table1_id = t1.id
where t2.id is null;
or
select t1.id from table1 as t1
where not exists (select 1
from table2 as t2 where t2.table1_id = t1.id);