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;
Related
Here are 2 tables.
Table 1
id value
1 3
2 2
3 3
4 1
5 4
6 3
Table 2
id
1
3
4
How do I get the ids that are in Table 2 which have the max value in Table 1?
Output:
id
1
3
I already tried the following to get the max value, but I cannot figure out how to use it in a single query to get the matching rows. Because I think I need to select from the same table I just inner joined.
select max(table1.value)
from table2
inner join table1 on table1.id = table2.id;
Here is one method:
select t2.id
from (select t2.*, rank() over (order by value desc) as seqnum
from table2 t2 join
table1 t1
on t2.id = t1.id
) t
where seqnum = 1;
Or, an alternative that puts all the ids on one row:
select group_concat(t2.id) as ids
from table2 t2 join
table1 t1
on t2.id = t1.id
group by t1.value
order by t1.value desc
limit 1;
You have a couple of options available without using window functions:
You can use a WHERE clause to select only id values that have a value equal to the MAX(value) from your query and an id that is in Table2:
SELECT t1.id
FROM Table1 t1
WHERE value = (
SELECT MAX(t1.value)
FROM Table2 t2
JOIN Table1 t1 ON t1.id = t2.id
)
AND id IN (SELECT id FROM Table2)
You can JOIN your query to Table1 and Table2 again, matching the value in Table1 and the id in Table2:
SELECT t1.id
FROM (
SELECT MAX(t1.value) AS max_value
FROM Table2 t2
JOIN Table1 t1 ON t1.id = t2.id
) t
JOIN Table1 t1 ON t1.value = t.max_value
JOIN Table2 t2 ON t2.id = t1.id
In both cases the output is
id
1
3
Demo on SQLFiddle
Too low to comment but from the SQL statement you gave, you just need to add the tableid in your select parameters.
select table2.id, max(table1.value)
from table2
inner join table1 on table1.id = table2.id;
I have the following query
SELECT table1.*,
table2.status
FROM table1
LEFT JOIN table2 ON table1.transactionid = table2.transactionid
WHERE table2.transactionid IS NULL --Doesn't exist in table2
OR table2.status = 2
It returns me the table1 row if there isn't any row under the same transactionid in table2 or there is a row but it has status = 2 (error)
Now i need to modify it to don't return any table1 rows if there is a row with the same transactionid and status = 1 (completed) in table2
So, there can be multiple error rows but if there is one that is completed then i shouldn't get any result
Thank you
Not exists comes to mind:
SELECT t1.*
FROM table1 t1
WHERE NOT EXISTS (SELECT 1
FROM table2
WHERE t1.transactionid = t2.transactionid AND
t2.status = 1
);
Using 'NOT IN'
SELECT t1.*
FROM table1 t1
WHERE t1.transactionid NOT IN (SELECT t2.transactionid
FROM table2 t2
WHERE t1.transactionid = t2.transactionid AND
t2.status = 1
);
OR USING LEFT JOIN
SELECT t1.*
FROM table1 t1
LEFT JOIN table2 t2
ON t1.transactionid = t2.transactionid
WHERE t2.transactionid IS NULL
I have the following table schema:
Table 1
-
field1
Table 2
-
field1 | field2
What I want to do is select field2 from the second table where field1 in the second table doesn't exist in the first table (field1).
I had this :
SELECT t2.field2
, t2.field1
FROM table1 t1
, table2 t2
WHERE t2.field1 != t1.field1
The problem is that this query will retrieve multiple repeated information from table2 if multiple rows apply in table1. I added DISTINCT and/or LIMIT but it still doesn't work. Any idea on how to do this?
You can use a subquery:
SELECT t2.field2, t2.field1 FROM table2 t2 WHERE t2.field1 NOT IN (SELECT t1.field1 FROM table1 t1);
This will give you all rows from table2 that have a field1 which is not in table1.
You can now use DISTINCT or LIMIT on the outermost query for any further processing.
You can use LEFT JOIN together with IS NULL check :
SELECT DISTINCT t2.field2
FROM table2 t2
LEFT JOIN table1 t1 ON t1.field1 = t2.field1
WHERE t1.field1 IS NULL
The title of your question is almost the command you need: 'NOT EXIST'. SQL is so simple. ;)
SELECT DISTINCT t2.field2
FROM Table2 t2
WHERE NOT EXISTS (SELECT * FROM Table1 WHERE t1.field1 = t2.field1)
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
I currently have a query
SELECT id FROM table1 WHERE {filters on table1} AND id NOT IN (SELECT table1ID FROM table2 WHERE condition = 0)
Table1 has a 1 - Many relationship with table2 and I'm looking for all the IDs that have no entries in table2 with condition=0.
Is there any way to rewrite this query without the inner select? I'm been scratching my head about it for a while now and any pointers would be welcome.
You can try something like
SELECT id
FROM table1 t1 LEFT JOIN
table2 t2 ON t1.ID = t2.table1ID
AND t2.Condition = 0
WHERE {filters on table1}
AND t2.table1ID IS NULL
Or just as good would be
SELECT id
FROM table1 t1
WHERE {filters on table1}
AND NOT EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.ID = t2.table1ID
ADN t2.condition = 0
)