mysql - conditional 'on clause' in mysql join - mysql

I try to establish a conditional 'on clause' in a mysql join.
If field1 is not empty, this should be used in the 'on claus',
but if empty, field2 instead should be used.
SELECT * FROM table1
JOIN table2
IF (field1!='') THEN (
ON table1.field1 = table2.field1
AND table1.field3 = table2.field3
)
ELSE (
ON table1.field2 = table2.field2
AND table1.field3 = table2.field3
)
END IF
Any idea if this is possible on how it could be done?
Edit:
I forgot to explain that both tables contain empty fields and I try to prevent that mysql uses these fields for the join, as that gives a (very) lot of joins, so my idea should be more like this:
SELECT * FROM table1
JOIN table2
IF (table1.field1!='' AND table2.field1!='') THEN (
ON table1.field1 = table2.field1
AND table1.field3 = table2.field3
)
ELSE (
ON table1.field2 = table2.field2
AND table1.field3 = table2.field3
)
END IF

Try this instead:
SELECT * FROM table1 t1
LEFT JOIN table2 t2
LEFT JOIN table2 t3
on t1.Field1=t2.Field1 and t1.Field3=t2.Field3
on t1.Field2=t3.Field2 and t1.Field3=t3.Field3
Then use a condition tho choose the field from table2 or table3.
Hope this helps you.
EDIT
To select the right field use this:
SELECT t1.*, IF(t2.field1 is null, t3.field1, t2.Field1) as Field1

I think you can do something like this:
SELECT * FROM table1
JOIN table2 ON
(
(table1.field1 = '' AND table1.field2 = table2.field2 AND table1.field3 = table2.field3)
OR
(table1.field1 != '' AND table1.field1 = table2.field1 AND table1.field3 = table2.field3)
)
(note: I have no idea how efficient or otherwise this approach is)

You should consider converting your INNER JOIN to a LEFT JOIN as already shown in other answer like
SELECT * FROM table1 t1
LEFT JOIN table2 t2
ON t1.field1 = t2.field1 AND t1.field3 = t2.field3
LEFT JOIN table2 t22
ON t1.field2 = t22.field2 AND t1.field3 = t22.field3
You can as well try something like below
SELECT t1.*
FROM table1 t1
JOIN table2 t2
ON t1.field1 = t2.field1 AND t1.field3 = t2.field3
OR t1.field2 = t2.field2 AND t1.field3 = t2.field3

Related

case usage in mysqli for selecting and joining

Hello all I am having a simple problem while selecting some data from table and then joining the table with another table depending on the value of field in table 1
like i have table1, table2 and table 3.
I want to
select field1,field2 and then check the value of field 3 if field 3 has value = 1 then select field1,field2,field3 from table 2 and join table 1 with table 2 on field1 and field1 else select field1,field2,field3 from table3 and join table1 with table2 on field1 and field1.
I now this can be done case but i am not so comfurtable with it please help me solve the problem ..
I think this can be achieved using a UNION call:
SELECT t2.f1, t2.f2, t2.f3
FROM table2 t2
INNER JOIN table1 t1
ON t1.f1 = t2.f1
WHERE t2.f3 = 1
UNION ALL
SELECT t3.f1, t3.f2, t3.f3
FROM table3 t3
INNER JOIN table1 t1
ON t1.f1 = t3.f1
Try this:
SELECT t1.field1 AS t1field1,
t1.field2 AS t1field2,
CASE WHEN t1.field3 = 1 THEN t2.field1 ELSE t3.field1 END AS t2t3field1,
CASE WHEN t1.field3 = 1 THEN t2.field2 ELSE t3.field2 END AS t2t3field2,
CASE WHEN t1.field3 = 1 THEN t2.field3 ELSE t3.field3 END AS t2t3field3
FROM table1 t1
LEFT JOIN table2 t2 ON t1.field1 = t2.field1
LEFT JOIN table3 t3 ON t1.field1 = t3.field1;

Inner join and delete not working in mysql

I have two tables table1 and table2. I want to delete from table1 based on a condition in table2.
I have the following mysql query:
DELETE FROM table1
INNER JOIN table2 ON table2.col1 = table1.col1
WHERE table2.col2 = '1'
This return a syntax error. Is there something wrong with the above syntax?
You need to specify the table you are deleting from:
DELETE table1
FROM table1 INNER JOIN
table2
USING (col1)
WHERE table2.col2 = '1';
Try this:
DELETE FROM table1
WHERE EXISTS(
SELECT 'C'
FROM table2
WHERE table2.col1 = table1.col1
AND table2.col2 = '1'
)
You could do something like:
DELETE FROM table1 WHERE col1 IN (select col1 from table2 WHERE table2.col2 = '1');

Can't figure error in a mysql query

$query = "SELECT
table1.first_name,
table1.id,
table1.profile_image
FROM table1,table2
WHERE table1.id = (
CASE
WHEN '$id' = table2.id1
THEN
table2.id2
WHEN '$id' = table2.id2
THEN
table2.id1
)
AND table2.case='done'";
This query is failing and I can't figure out why... What I want is to obtain a join on ids between table1 and table2. The thing is table2 has two id fields and I don't know if the Id I want is in field id1 or id2. Then join the other id (NOT $id, but the partner from $id in table2) to the id in table1...
You know the right syntax for a case construction is:
CASE
WHEN ...
THEN ....
WHEN ...
THEN ....
ELSE ...
END
Notice the END at the .... end ;)
The lack of end in the case is the obvious problem with the query. However, it would be better written as:
SELECT t1.first_name, t1.id, t1.profile_image
FROM table1 t1 join
table2 t2
on ('$id' = table2.id1 and t1.id = t2.id2) or
('$id' = table2.id2 and t1.id = t2.id1)
WHERE t2.case = 'done';
Note the use of explicit join syntax, the use of table aliases, and the replacement of case with basic boolean logic.
Select first_name, id, profileImage From Table1
inner join (
Select id1 as t2id From Table2 where `case` = 'done'
union
Select id2 From Table2 Where `case` = 'done') t2Ids
) On Table1.id = t2Ids.t2id
Where t2Ids.t2Id = $id
Would be another way to do it.
Try this:
SELECT table1.first_name,table1.id,table1.profile_image
FROM table1,table2
WHERE (table1.id = table2.id1 or table1.id = table2.id2)
AND table2.case='done'

mysql return matching other rows when one table returns 0

Simply put:
SELECT
table1.field1,
table2.field2,
table3.field_WHAT
FROM
table1,
table2,
table3
WHERE
table1.field1 = 'HELP'
and table2.field1 = table1.field1
and table3.field2 = table2.field2
I still want all matching rows from table1 & table2, even when no matching record found for table3.field2 ... and ideally if no match, table3.field_WHAT would return "" or NULL.
Try this:
SELECT
table1.field1,
table2.field2,
table3.field_WHAT
FROM
table1
INNER JOIN
table2 ON table1.field1 = table2.field1
LEFT OUTER JOIN
table3 ON table3.field2 = table2.field2
WHERE
table1.field1 = 'HELP'
This is a classic case for an outer join:
SELECT
table1.field1,
table2.field2,
table3.field_WHAT
FROM
table1,
JOIN
table2 ON table2.field1 = table1.field1
OUTER JOIN
table3 ON table3.field2 = table2.field2
WHERE
table1.field1 = 'HELP'

MySql Inner join based on non empty columns

I have Two tables :
Table1, Table2
I have to write a SQL query which has inner join on three columns based
on <> 0 condition
on these tables.
My condition is
if(table1.ID != 0), then inner join on table1.ID = table2.ID else,
if(table1.MemberID != 0), then inner join on table1.MemberID = table2.MemberID else,
inner join on table1.PersonID = table2.PersonID
You can use a conditional statement (case when for example) in a join clause.
select t1.*, t2.*
from table1 t1
inner join table2 t2 on
(case when
t1.ID != 0
then t1.ID =t2.Id
else
case when t1.MemberId !=0
then t1.MemberId = t2.MemberId
else
t1.PersonId = t2.PersonId
end
end)
see SqlFiddle
This should work.
select *
from Table1, Table2
where table1.Personid = table2.personId
and (((Table1.id != 0) and (Table1.id = table2.Id))
or ((Table1.memberid != 0) and (Table1.memberid = table2.memberId)))