how to use in clause into inner join - mysql

I need a query like that ;
select a.*, b.column1 from table1 a
inner join table2 b on (b.id in (id_array))
This query returns nothing.
What I want to do: I retrieve the group of ids from table1.Then get the name column of these ids from table2. So, I use inner join for this.

Shouldn't just be the join?
select a.*, b.* from table1 a
inner join table2 b on b.id = a.id

select *
from table1, table2
where find_in_set(id, ids) > 0

select a.* , b.* from table1 a inner join table2 b on a.id = b.id

I assume that a.ids is some kind of id list, so you could use the find_in_set() function:
select *
from table1 a
join table2 b on find_in_set(b.id, a.ids) > 0

Related

OR'ing two INNER JOIN's in MySQL

Is it possible to OR two separate INNER JOIN's so that the result set contains data from either of the two INNER JOIN's? For instance, is the following possible in MySQL.
SELECT * FROM table1
(INNER JOIN table2 ON table1.column_name=table2.column_name)
OR
(INNER JOIN table3 ON table1.column_name=table3.column_name)
No you cant do this way, one way is to use union
SELECT * FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name
union
SELECT * FROM table1 INNER JOIN table3 ON table1.column_name=table3.column_name
You can use UNION:
SELECT * FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name
UNION DISTINCT
SELECT * FROM table1 INNER JOIN table3 ON table1.column_name=table3.column_name
As described at https://dev.mysql.com/doc/refman/4.1/en/union.html, UNION combines the results of several selects.
you need use where
select * from table1
where
(table1.column_name = (select table2.column_name from table2 inner join table1.column_name as on table1.column_name = table2.column_name))
OR
(table1.column_name = (select table3.column_name from table3 inner join table1.column_name as on table1.column_name = table3.column_name))
work perfect here!

Get distinct value from join query (Join Table)

How get a distinct value form more than one table (inner join query).
Eg,
select a.id,b.name,c.address
from table1 a
inner join table2 b on (a.id = b.row_id)
inner join table3 c on (a.id = c.ticket_id)
where c.status = 'open';
Here the scenario is for example, two rows contain the same a.id value so how to get the distinct value from a.id.
Somebody help me that how to get?
just add Distinct ...
select DISTINCT a.id,b.name,c.address
from table1 a
inner join table2 b on (a.id = b.row_id)
inner join table3 c on (a.id = c.ticket_id)
where c.status = 'open';
i think this is works fine..
if you need only one record distinct then it should be like this...
SELECT DISTINCT(cat_id) FROM PRODUCTS WHERE brand_id = 'sony'

Inner join of 2 tables with the same ID

I have a table(T1) in which 2 columns(X and Y) are id's. The name's of these corresponding id's are in another table (T2) with the column name.
Suppose I was only using X then, a simple Inner join would have solved my problem in getting the name.
Such as
Select T1.somedata,T1.somedata1,T2.name from T1
Inner Join T2 ON T1.X=T2.id
But,what if I want the name's to be resolved for the T1.Y also?, which name would the Inner Join resolve it to ??
Select T1.somedata,T1.somedata1,T2.name from T1
Inner Join T2 ON T1.X=T2.id
Inner Join T2 ON T1.Y=T2.id
The above query is wrong, I know. Can I get the names of those corresponding to both T1.Xand T1.Y with an INNER Join?
-Beginner
I suggest always add aliases to tables and columns. So you will be sure which data are selected.
select
T1.somedata,
T1.somedata1,
T2X.name as XName,
T2Y.name as YName
from T1 as T1
inner join T2 as T2X on T2X.id = T1.X
inner join T2 as T2Y on T2Y.id = T1.Y
This selects both names as separate columns:
Select T1.somedata,T1.somedata1,T2a.name, T2b.name
from T1
Inner Join T2 as T2a ON T1.X=T2a.id
Inner Join T2 as T2b ON T1.Y=T2b.id
The following would generate two records in the result set:
Select T1.somedata, T1.somedata1, T2.name
from T1
Inner Join T2 ON T1.X=T2.id Or T1.Y=T2.id
you need to join table T2 twice and supply aliases on the names to avoid ambiguity.
SELECT a.*,
b.name as NameB,
c.name as NameC
FROM T1 a
INNER JOIN T2 b
ON a.x = b.id
INNER JOIN T2 c
On a.y = c.id
You need to join table T2 twice and supply aliases on the names to avoid ambiguity.
SELECT a.*,
b.name as NameB,
c.name as NameC
FROM T1 a
INNER JOIN T2 b
ON a.x = b.id
INNER JOIN T2 c
On a.y = c.id

MySQL syntax for JOIN depending on condition

What would be a syntax of the following query:
Get all columns from Table1 and JOIN Table2 if matching reference (Table1ID) exists, otherwise JOIN Table3.
Simplified DB structure is more or less as below
Table1
ID Type
1 std
Table2
ID Table1ID Title Language
1 1 Test en
Table3
ID Table1ID Title Language Flag
1 1 Other en 1
Also, I now realized that Table3 will have multiple entries that refer to single Table1.id. How to limit it to return only the latest entry (with highest id) for every result?
If you don't want an entire separate set of columns for each join, this may be what you're looking for:
SELECT *
FROM (
SELECT a.ID AS Table1ID, a.Type, b.ID, b.Title, b.Language, NULL AS Flag
FROM Table1 a
JOIN Table2 b ON a.ID = b.Table1ID
UNION ALL
SELECT a.ID, a.Type, c.ID, c.Title, c.Language, c.Flag
FROM Table1 a
LEFT JOIN Table2 b ON a.ID = b.Table1ID
JOIN Table3 c ON a.ID = c.Table1ID
JOIN (
SELECT MAX(id) AS maxid
FROM Table3
GROUP BY Table1ID
) d ON c.ID = d.maxid
WHERE b.ID IS NULL
) a
ORDER BY a.Table1ID
SQLFiddle Demo
this is one way to do it.
select table1.id, table1.type, ifnull(table2.title, table3.title)
from table1
left join table2 on table1.id = table2.table1ID
left join table3 on table1.id = table3.table1ID

Select all colums from a table and some from another using left join

How do you select all the columns from one table and just some columns from another table using left JOIN? In MySQL
This will select all columns from table1 (alias a) and only id and name from table2 (alias b)
select a.*, b.id, b.name
from table1 a
inner join table2 b on b.id = a.id