MySQL LEFT JOIN in CASE? - mysql

I have a table where I want to join different tables depending on the value of one column, like so (this doesn't work, but it's my example):
SELECT * FROM table1
JOIN (CASE WHEN table1_column1=1 THEN table2 ON table2_column1=table1_column2 END)
WHERE table1_column3='hello'
All in all there are gonna be 4 values in the column, calling other tables. Is this doable?
Edit: I think I need to clarify what I'm after. Depending on the value of table1_column1, I want the JOIN to fetch a specific table and column. For example, if t1c1=1 it should join table2_column1 on table1_column2. If, however, t1c1=2 it should join table5_column1 on table1_column2. Etc, etc, etc.
Again - is this doable? It's easily scripted if I use two separate queries. I just want to use one query, however.

SELECT * FROM
table1 JOIN table2 ON table2_column1=table1_column2 AND table1_column1=1
UNION
SELECT * FROM
table1 JOIN table3 ON table2_column1=table1_column2 AND table1_column3='hello'
it might work

SELECT * FROM table1
JOIN table2 ON table2_column1=table1_column2 and table1_column1=1
WHERE table1_column3='hello'

Related

How to do a join on 2 tables, but only return the data for one table?

I am not sure if this is possible. But is it possible to do a join on 2 tables, but return the data for only one of the tables. I want to join the two tables based on a condition, but I only want the data for one of the tables. Is this possible with SQL, if so how? After reading the docs, it seems that when you do a join you get the data for both tables. Thanks for any help!
You get data from both tables because join is based on "Cartesian Product" + "Selection". But after the join, you can do a "Projection" with desired columns.
SQL has an easy syntax for this:
Select t1.* --taking data just from one table
from one_table t1
inner join other_table t2
on t1.pk = t2.fk
You can chose the table through the alias: t1.* or t2.*. The symbol * means "all fields".
Also you can include where clause, order by or other join types like outer join or cross join.
A typical SQL query has multiple clauses.
The SELECT clause mentions the columns you want in your result set.
The FROM clause, which includes JOIN operations, mentions the tables from which you want to retrieve those columns.
The WHERE clause filters the result set.
The ORDER BY clause specifies the order in which the rows in your result set are presented.
There are a few other clauses like GROUP BY and LIMIT. You can read about those.
To do what you ask, select the columns you want, then mention the tables you want. Something like this.
SELECT t1.id, t1.name, t1.address
FROM t1
JOIN t2 ON t2.t1_id = t1.id
This gives you data from t1 from rows that match t2.
Pro tip: Avoid the use of SELECT *. Instead, mention the columns you want.
This would typically be done using exists (or in) if you prefer:
select t1.*
from table1 t1
where exists (select 1 from table2 t2 on t2.x = t1.y);
Although you can use join, it runs the risk of multiplying the number of rows in the result set -- if there are duplicate matches in table2. There is no danger of such duplicates using exists (or in). I also find the logic to be more natural.
If you join on 2 tables.
You can use SELECT to select the data you want
If you want to get a table of data, you can do this,just select one table date
SELECT b.title
FROM blog b
JOIN type t ON b.type_id=t.id;
If you want to get the data from two tables, you can do this,select two table date.
SELECT b.title,t.type_name
FROM blog b
JOIN type t ON b.type_id=t.id;

Merge two tables to one and remove duplicates

I have 2 tables in the same database.
I want to merge them based on the common id column. Because the tables are too huge I am not sure if there are duplicates.
How is it possible to merge these two tables into one based on the id and be sure that there are no duplicates?
SELECT *
FROM table1,table2
JOIN
GROUP BY id
What do you mean by merging two tables? Do you want records and columns from both the tables or columns from one and records from both?
Either way you will need to change the join clause only.
You could do a join on the columns you wish to
SELECT DISTINCT *
FROM table1 tb1
JOIN table2 tb2
ON table1.id = table2.id
Now if you want columns from only table1 do a LEFT JOIN
If you want columns from only table2 then a RIGHT JOIN
If you want columns from both the tables, use the query as is.
DISTINCT ensures that you get only a single row if there are multiple rows with the same data (but this distinct will check values for all columns in a row whether they are different or the same)
Union won't help if both tables have different number of columns. If you don't know about joins then use a Cartesian product
select distinct *
from table1 tb1, table2 tb2
where tb1.id = tb2.id
Where id is the column that is common between the tables.
Here if you want columns from only table1 do
select distinct tb1.*
Similarly replace tb1 by tb2 in the above statement if you just want table2 columns.
select distinct tb2.*
If you want cols from both just write '*'
In either cases I.e. joins and products said above if you need selective columns just write a table alias. E.g.
Consider :
table1 has id, foo, bar as columns
table2 has id, name,roll no, age
you want only id, foo, name from both the tables in the select query result
do this:
select distinct tb1.id, tb1.foo, tb2.name
from table1 tb1
join table2 tb2
on tb1.id=tb2.id
Same goes for the Cartesian product query. tb1, tb2 are BTW called as a table aliases.
If you want data from both the tables even if they have nothing in common just do
select distinct *
from table1 , table2
Note that this cannot be achieved using a join as join requires a common column to join 'on'
I am not sure What exactly do you want but anyway, this is your code
SELECT *
FROM table1,table2
JOIN
GROUP BY id
i just edit your query
SELECT *
FROM table1 JOIN table2
on table2.id = table1.id
GROUP BY table1.id // here you have to add table
//on which you will be group by at this moment this is table1
Try UNION:
https://dev.mysql.com/doc/refman/5.0/en/union.html
IT is very simple. Hope it will help.
Also you should have a look at "DISTINCT".

SQL: Unioning 2 tables and then joining on another. Is there a better way of doing this?

I have to maintain an application who's database is a little haywire and I had the following challenge to join 3 tables:
table1
timestamp,zid,aaa
table2
timestamp,zid,bbb
table3
id,some,other,data
So table 1 and 2 need to be joined and sorted on their timestamp columns. I also need to preserve table1's aaa and table2's bbb column and be able to distinguish whether its an aaa or a bbb. Then I need to join it to another table via their zid column.
Here is my working sql for this:
SELECT *
FROM
table3
LEFT JOIN
(
SELECT table1.timestamp AS timestamp,
table1.zid,
table1.aaa AS responses,
'aaas'
FROM table1
UNION ALL
SELECT table2.timestamp AS timestamp,
table2.zid,
table2.aaa AS responses,
'bbbs'
FROM table2
) aaasAndbbbs ON table3.id = aaasAndbbbs.zid
ORDER BY timestamp ASC;
The above sql does exactly what I want, but I want to know if there is a better way. Here I use the "aaas" column to distinguish whether it is an aaa or a bbb- which seems a little hacky. Is there a preferable way to do what I want?
The alternative would be to do each one separately and you will see the LEFT JOIN to table 3 is duplicated. I suspect however that if you look at the Execution plan for both your version and my version that they will be the same. The SQL Server Optimiser should be clever enough to figure out that both are doing the same thing and hence execute both in the same way. I find this slightly more readable than yours but that's my preference!
SELECT table1.timestamp AS timestamp,
table1.zid,
table1.aaa AS responses,
'aaas'
FROM table3
LEFT JOIN table1 on table3.id = table1.zid
UNION
SELECT table2.timestamp AS timestamp,
table2.zid,
table2.aaa AS responses,
'bbbs'
FROM table3
LEFT JOIN table1 on table3.id = table2.zid
ORDER BY timestamp ASC;

Joining a subselect to a table in sql

Is it possible to join the results of a SELECT with another table.
Like this:
SELECT * FROM table1 LEFT OUTER JOIN (SELECT * FROM table 2)
I know I need to link the column but am not sure how. Is this possible?
You need to know what columns you are joining on. Assuming they are called ID in both tables, something like this would work:
SELECT *
FROM table1 t1
LEFT OUTER JOIN (SELECT * FROM table 2) t2 on t1.ID = t2.ID
Note that rather than using *, you should name the columns you need explicitly. This will give a more efficient query if you do not need all of the data, and will also prevent duplicate column names from being returned.
You can do this. The code would be something like:
(SELECT id as leftid, [other fields] FROM table1) LEFT OUTER JOIN (SELECT id rightid, [other fields] FROM table2) ON (leftid=rightid)
I didn't test it, though...

How to select two seperate non-overlapping tables in MySQL

I'm looking for a query to select rows from two different tables, keeping the column names the same (I did find one result here for selecting from two different tables, but it merged the column names to have an easier query). I need to keep the original column names, but have two different tables existing within the new, larger table. There are no overlapping columns between the two tables.
A picture, to visualise:
So, how can I do this? I know the query will probably be quite convoluted, but anything half-decent is probably going to be better than my current attempt:
SELECT t1.* , t2.*
FROM table1 t1 RIGHT OUTER JOIN table2 t2
ON r.someColumn1 = rc.someColumn2
UNION
SELECT t1.* , t2.*
FROM table1 t1 LEFT OUTER JOIN table2 t2
ON r.someColumn1 = rc.someColumn2
This does work, but only as long as there are no cases where someColumn1 = someColumn2 - which can happen quite easily, of course.
Any help is appreciated, and I apologise for what is probably a very silly question to which the smart answer is "don't do it, you fool!".
You can set your join criterion to never match:
SELECT t1.* , t2.*
FROM table1 t1 RIGHT OUTER JOIN table2 t2
ON 1 = 0
UNION
SELECT t1.* , t2.*
FROM table1 t1 LEFT OUTER JOIN table2 t2
ON 1 = 0
I don't have MySQL to test, but it works in SQL Server.
Edit: my first answer was wrong:
select * from Events
left join GroupList on ID=null
union
select Events.*,GroupList.* from GroupList
left join Events on GID=null
In the above GID and ID are keyfields in the tables.