Condition based select and join queries - mysql

Query 1:
SELECT if(COUNT(0),1,0) as 'IsPresent'
FROM table1
WHERE Id=1500;
Query2:
If IsPresent is 1, then
select t2.mark,t2.age from table2 t2,table1 t1
where t1.ID=t2.ID order by t1.ID;
If IsPresent is 0, then
select mark,age from table2;
ie. if entry is present in a table, i need to join else i don't need to join.
Is there any way we can achieve this with a single mysql select query?

I think you can union the two different query cases which would look like:
SELECT T2.MARK, T2.AGE
FROM TABLE1 T1, TABLE2 T2
WHERE
T1.ID=T2.ID AND
T1.ID=1500
UNION
SELECT MARK, AGE
FROM TABLE1
WHERE
NOT ID=1500

SELECT t2.mark, t2.age
FROM table2 t2
JOIN table1 t1
ON t1.id = t2.id
WHERE EXISTS
( SELECT *
FROM table1
WHERE id=1500
)
UNION ALL
SELECT t2.mark, t2.age
FROM table2 t2
WHERE NOT EXISTS
( SELECT *
FROM table1
WHERE id=1500
)
which can be simplified to:
SELECT t2.mark, t2.age
FROM table2 t2
LEFT JOIN table1 t1
ON t1.id = t2.id
AND EXISTS
( SELECT *
FROM table1
WHERE id=1500
)

Related

JOIN a table by SELECT subquery with LIMIT 1 and WHERE clause matching column values outside of it

I'm trying to JOIN a table with a subquery to limit data to only 1 last row, matching certain values from other FROM or JOINed tables:
SELECT
t1.column1,
t1.column2,
t2.column1,
t3.column2
FROM
table1 t1
JOIN
(
SELECT column1
FROM table2
WHERE t1.column2 > table2.column1
ORDER BY table2.date DESC
LIMIT 1
) t2
JOIN table3 t3 ON t2.column1=t3.column2
Getting an error: Unknown column t1.column2 in 'where clause'. Seems I can't address other columns inside the subquery.
Is what I'm trying to do possible?
If so, what am I doing wrong / what other way could I try this?
In MySQL, implicit and explicit joins together in a single query create the problem. You can not use an implicit JOIN based on a nested SELECT - FROM - WHERE query when there is another explicit JOIN present.
Try below.
SELECT *
FROM table1 t1
INNER JOIN table2 t2 ON t1.column2 > t2.column1
LEFT OUTER JOIN table3 t3 ON t2.column1=t3.column2
You may re-write your query as -
SELECT t1.column1,
t1.column2,
t2.column1,
t3.column2
FROM table1 t1
JOIN (SELECT column1
FROM table2
ORDER BY table2.date DESC
LIMIT 1) t2 ON t1.column2 > t2.column1
JOIN table3 t3 ON t2.column1=t3.column2
The solution for me was:
SELECT
t1.column1,
t1.column2,
t2.column1,
t3.column2
FROM
table1 t1
JOIN table2 t2 ON t2.id =
(
SELECT id
FROM table2
WHERE t1.column2 > table2.column1
ORDER BY table2.date DESC
LIMIT 1
)
JOIN table3 t3 ON t2.column1=t3.column2
More info on:
CROSS/OUTER APPLY in MySQL
https://dba.stackexchange.com/a/170760/13156

sql to extract same columns from 3 different tables

Hi i have 3 tables i want to extract same columns from 3 tables is this better way to write Select query.
select * from
(
select col1,
select id1 from testid1 where name=pnrtable1.name,
col3 from table1
union all
select coltab1,
select newid2 from testid2 where name=pnrtable2.name,
coltab3 from table2
union all
select namecol1,
select id3 from testid3 where name=pnrtable3.name,
namecol3 from table3
)
Not sure but I think you are after something like this....
select * from
(
select t1.col1, t2.id1 , t1.col3 from table1 t1
INNER JOIN testid1 t2 ON t1.name = t2.name
union all
select t1.coltab1, t2.newid2, t1.coltab3 from testid2 t1
INNER JOIN table2 t2 ON t1.name=t2.name
union all
select t1.namecol1, t2.id3, t1.namecol3 from testid3 t1
INNER JOIN table3 t2 ON t1.name=t2.name
) A

SQL join result fulltext

I have two tables, which I will call table1 and table2. Table1 has 2 fields, id and auth, table2 also has two fields, id and keywords. Note that id of table1 and table2 match.
This is my query:
SELECT id, MATCH(keywords) AGAINST('example') FROM table2 WHERE MATCH(keywords) AGAINST('example')
How am I going to exclude results where the auth (table1) of that same id is not 1?
SELECT id, MATCH(keywords) AGAINST('example')
FROM table2 t2
WHERE MATCH(keywords) AGAINST('example')
AND NOT EXISTS (select 1 from table1 t1 where t1.id = t2.id and t1.auth != 1)
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.id != t2.id

select data from subquery mysql

select t1.table1 from table1 as t1
where t1.column1
in
(
select t2.column2 from table2 as t2
join
table3 as t3 on t2.column1=t3.column1
where t3.columnx=5
);
Above is the mysql query i am firing. Wanted some data from the subquery tables also.
For example say columnxy from table t2.
query that fails
select t1.table1,t2.columnxy from table1 as t1
where t1.column1
in
(
select t2.column2 from table2 as t2
join
table3 as t3 on t2.column1=t3.column1
where t3.columnx=5
);
If i add them with select of the outer query gives error "unknown column" which does make sense.
Is the right way or should rewrite query with joins?
Rewrite the query with joins:
SELECT t1.table1, t.columnxy
FROM table1 AS t1 JOIN (
SELECT t2.column2, t2.columnxy
FROM table2 AS t2 JOIN table3 AS t3 USING (column1)
WHERE t3.columnx = 5
) t ON t1.column1 = t.column2
Or:
SELECT t1.table1, t2.columnxy
FROM table1 AS t1
JOIN table2 AS t2 ON t1.column1 = t2.column2
JOIN table3 AS t3 ON t2.column1 = t3.column1
WHERE t3.columnx = 5
The t2 is not available at that point. You should use a join for this. Using t1.column1=t2.column2 should do it.

Select in where clause, access to current parent select columns

I have a query which contains a select statement in it's where clause. My question is now, how can I access the parent's select's data.
Example:
select * from TABLE_1 as t1 INNER JOIN TABLE_2 as t2
where (... and ...) OR
(not exists(select * from TABLE_3 as t3
inner join TABLE_1 ON t3.t1_id = t1.id
The last line is where the error occurs: t1.id is not a column.
How can I access the current value from the table t1?
I'm using MySql 5.1
SELECT
*
FROM
TABLE_1 as t1
INNER JOIN TABLE_2 as t2 ON
t2.PK = t1.FK --Whatever your keys are
WHERE
(... and ...)
OR
(
NOT EXISTS (select * from TABLE_3 as t3 WHERE t3.t1_id = t1.id)
)
First of all, you need to declare what you will JOIN TABLE_2 on TABLE_1.
SELECT *
FROM TABLE_1 AS t1
INNER JOIN TABLE_2 AS t2 ON t2.t1_id = t1.id
t1.id = t2.t1_id is just an example, you will need to decide which columns you wish you join on. Then in your WHERE clause, you do not need to INNER JOIN on TABLE_1 again as you are already selecting from it.
SELECT *
FROM TABLE_1 AS t1
INNER JOIN TABLE_2 AS t2 ON t2.t1_id = t1.id
WHERE (... AND ...) OR
(
NOT EXISTS
(
SELECT *
FROM TABLE_3 AS t3
WHERE t3.t1_id = t1.id
)
)