Can't figure error in a mysql query - mysql

$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'

Related

SQL query to find unreferenced records

I have two tables bound through an ID field:
table1: id, name, type
table2: id, id_table1, date, status
I have to collect all the records of the table1 that have a certain value of type field and that are not been referenced in table2 plus all the records of table1 referenced in table2 that have a certain status field value.
For the first part if I remember correctly I can use the LEFT JOIN command:
LEFT JOIN table1.name
LEFT JOIN table2
ON table2.id_table1 = table1.id
WHERE (table1.value = 'value1') AND (table2.id_table1 IS NULL);
but for the second part I'm getting lost...
I'm using MySQL 5.6 and I would like to define a View to handle this.
SELECT t1.*, t2.*
FROM table1 t1
LEFT JOIN table2 t2
ON table2.id_table1 = table1.id
WHERE (t1.type= 'value1' AND t2.id IS NULL)
OR (t2.status = 'certain status' )
I would think you could just change the WHERE to:
WHERE (table1.value = 'value1')
AND (table2.id_table1 IS NULL
OR
([the other table2 status criteria)
)
;
You can try this...
SELECT T1.*,T2.*
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T1.Value = 'value1' AND T2.id_table1 IS NULL
UNION
SELECT T1.*,T2.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T2.Status= 'Status Criteria'

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');

mysql - conditional 'on clause' in mysql join

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

Select statement within if condition not working with additional parameters

I'm trying to do this
SELECT
table1.*,
table2.id as t2id
FROM
table1 as t1
INNER JOIN table2 as t2
ON t2.field1 = t1.field2
AND t1.field2 = 'value'
AND IF(SELECT COUNT(*) FROM table2 WHERE id = 10 > 0)
It error says
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
'SELECT COUNT(*) FROM table2 WHERE id = 10 > 0) LIMIT ' at line 1
I know the error is with the if condition as when I remove it, it works, but my thinking is the select will return a null value if it was not successful i.e. it didn't find anything in the table with id 10 from table2.
You're missing the "then" part of your if.
It has too look like IF (condition, then, else), but you're just doing the condition without any output.
Try it that way:
AND IF((SELECT COUNT(*) FROM table2 WHERE id = 10) > 0, 'true', 'false')
Try removing the IF.
SELECT
table1.*,
table2.id as t2id
FROM
table1 as t1
INNER JOIN table2 as t2
ON t2.field1 = t1.field2
AND t1.field2 = 'value'
AND (SELECT COUNT(*) FROM table2 WHERE id = 10) > 0;
Try this (make sure that you exec the queries on the same session):
SELECT COUNT(*) INTO #COUNTER FROM table2 WHERE id = 10 ;
SELECT
table1.*,
table2.id as t2id
FROM
table1 as t1
INNER JOIN table2 as t2
ON t2.field1 = t1.field2
AND t1.field2 = 'value'
AND #COUNTER > 0 ;

UPDATE table1 SET column1 = (SUM(table2{& table3} WHERE table2_id1 = id1) WHERE id1 = table2_id1

I'd like to update table1 based upon a sum principally applied on table2 but including a single value from table 3.
table2 has a column that's FKd to table1's id column, and the sum is based upon them matching.
UPDATE table1, table2
SET table1.column1 =
(SELECT SUM( (SELECT constant FROM table3) +
(SELECT table2.sum_number
WHERE table2.table2_id1 = table1.id) ) )
WHERE table1.id = table2.table2_id1;
That doesn't work for me.
Many thanks in advance!
EDIT: Error Given
#1064 - You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near
'WHERE table2.table2_id1 = table1.id) ) ) WHERE table1.id = table2.table2_id1;'
UPDATE table1, table2
SET table1.column1 =
(
SELECT SUM(
(SELECT constant FROM table3) +
(SELECT table2.sum_number *** WHERE table2.table2_id1 = table1.id)
)
)
WHERE table1.id = table2.table2_id1;
There is no "FROM table2,table1" in the area marked with astericks above.
Try this:
UPDATE
table1 t1
INNER JOIN
table2 t2 ON t2.table2_id1 = t1.id
SET
t1.column1 = (SELECT constant FROM table3) + t2.sum_number
try this:
Update table1 t1 join table2 t2 on t1.id = t2.table2_id1
SET t1.column1 = (SELECT constant FROM table3) + t2.sum_number