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 ;
Related
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;
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'
Ok. I have some data in one table, that references on multiple occasions some data in another table.
Table1 - main client table
Table2 - user defined fields
Say I have a query that shows a client id from Table1 and all attached / used "used defined fields" from Table2
SELECT t1.Id, t2.udf
FROM Table1 t1
JOIN Table2 t2 ON t1.Id = t2.Index
WHERE t1.EndDate IS NULL AND
t1.Id = '1234.9876' AND
I would get the following for a result...
ID UDF
1234.9876 100
1234.9876 110
1234.9876 118
1234.9876 124
1234.9876 198
1234.9876 256
Now, say I wanted to query this same thing, and get ONLY the ID of the Client, but ONLY IF a value for t2.udf equaling '194' did not exist. So, I would simply get
ID
1234.9876
...as a result.
Make the join a LEFT join and filer where t2.Index is null
SELECT t1.Id
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Id = t2.Index
AND t2.UDF = 194 -- has to be before where clause
WHERE t2.Index IS NULL
AND t1.EndDate IS NULL
AND t1.Id = '1234.9876' -- not sure if you want this part
Another way by using NOT EXISTS
SELECT t1.Id
FROM Table1 t1
WHERE NOT EXISTS (SELECT 1 FROM Table2 t2 WHERE t1.Id = t2.INDEX
AND t2.UDF = 194)
AND t1.EndDate IS NULL
AND t1.Id = '1234.9876'
See also JOINS
You can add AND t2.udf not in (select udf from table2 where udf <> '194').
But #SQLMenace solution is better
This should do it.
SELECT DISTINCT t1.Id
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Id = t2.Index
WHERE t2.UDF NOT IN (194)
AND t2.Index IS NULL
Select DISTINCT gives you unique entries that satisfy the other conditions, and the first where clause
t2.UDF NOT IN (194)
Normall would return all the rows for the t1 where the t2.UDF is not 194, but it is limited by the Select Distinct to give you only distinct id's
Try the following:
SELECT t1.Id
FROM Table1 t1
JOIN Table2 t2 ON t1.Id = t2.Index
WHERE t1.EndDate IS NULL AND
t1.Id = '1234.9876' AND
t2.udf <> '194'
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
(SELECT * FROM table1
INNER JOIN table2
ON table1.id = table2.id)
AS t1
JOIN (SELECT ROUND(RAND() * (SELECT MAX(table1.id) FROM table1)) AS id)
AS t2
WHERE t1.id >= t2.id
LIMIT 1)
I try to use RAND() max(id) to get a random mysql result, but get a #1064 error.
#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 'AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(table1.id) FROM table1))' at line 1
Where is the problem? thanks.
(assuming that this code snippet is an entire query)
May be wrong but your statement does not have a SELECT ... in short it looks like this:
t1 JOIN t2 WHERE ...
There is no SELECT something FROM t1 JOIN t2 WHERE ...
Not sure if I make myself clear...
Addendum:
Not sure what you re trying to achieve, but this code bellow returns random IDs from your tables (variation of your query) so perhaps you can use it. A bit messy perhaps but then again I have no idea what are you trying to achieve :).
SELECT * FROM
(SELECT table1.id as id1
FROM table1
INNER JOIN table2
ON table1.id = table2.id) as t1
JOIN (
(SELECT ROUND(RAND() * (SELECT MAX(table1.id) FROM table1)) AS id2)
AS t2 )
WHERE t1.id1 >= t2.id2
LIMIT 1
You can select id1 or id2 instead of *, depending on what is your goal...
(assuming that this code snippet is part of a bigger query)
The problem is this subquery:
(SELECT * FROM table1
INNER JOIN table2
ON table1.id = table2.id)
AS t1
Running it alone:
SELECT * FROM table1
INNER JOIN table2
ON table1.id = table2.id ;
will give no error but it will show/return at least 2 columns with same name (id). This is causing the conflict when you are trying to include it as a subquery in a bigger query.