Mysql merging two queries into one - mysql

Trying to merge below two queries into one in the most "correct/efficient" way:
select id from TABLE1 where apple='green'
while{
select * from TABLE2 where id=[id from TABLE1 above]
while{
Eg query one will return a number of id's which in turn will return a number of rows in query two.

You can try to use EXISTS subquery
SELECT *
FROM TABLE2 t2
WHERE EXISTS(
SELECT 1
FROM TABLE1 t1
WHERE t1.apple='green' AND t1.id = t2.id
)

Related

SQL query to get 2 values from table 1 and join all possible option from table 2

I want to get value from table1 and join all matching value from table2. The table1 has to be limited to 2 rows, but expecting output should own all matching values for those two ids.
How can I achieve this?
You would use a subquery:
select t1.*, t2.*
from (select t1.*
from table1 t1
limit 10
) t1 left join
table2 t2
on t1.id = t2.table1_id;
Note: This returns two arbitrary rows. Normally, you would have an order by to better specify the rows. And use order by rand() for random rows.
if you want all the value in join for only two row of table 1 you can use a subqiuery with limit 2
select b.id, a.value, b.value2, b.table1_ID
from (
select * from table1
limit 2
) a
inner join table2 on aid = b.table1_ID

Alternative to except in MySQL

I must write a Query like this in MySQL:
SELECT *
FROM Tab1
EXCEPT
SELECT *
FROM Tab1
WHERE int_attribute_of_Tab1>0
but MySQL doesn't support the keyword EXCEPT.
Is there a standard mode to use correctly another operator that simulate the except in MySQL?
You could use NOT IN
SELECT *
FROM Tab1
WHERE id NOT IN (
SELECT id
FROM Tab1
WHERE int_attribute_of_Tab1>0
)
Try this
SELECT *
FROM Tab1
WHERE [....] NOT EXISTS
(SELECT *
FROM Tab1
WHERE int_attribute_of_Tab1>0)
A couple of definitions
SqlServer https://learn.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-except-and-intersect-transact-sql EXCEPT
Returns any distinct values from the query to the left of the EXCEPT operator that are not also returned from the right query.
PLsql https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries004.htm MINUS
statement combines results with the MINUS operator, which returns only unique rows returned by the first query but not by the second
A pedantic translation to mysql would be
SELECT distinct t1.*
FROM Tab1 as t1
left outer join
(SELECT *
FROM Tab1
WHERE int_attribute_of_Tab1>0) as t2 on t1.id = t2.id
where t2.id is null;
Assuming there is an id column, And I wouldn't like to use distinct on a lot of columns.
You can use multiple NOT IN operators combined with AND operators for multiple columns.
For example:
SELECT col1, col2 FROM table1 WHERE col1 NOT IN(SELECT col1 FROM table2) AND col2 NOT IN(SELECT col2 FROM table2)...;
Since MySQL version 8.0.31 update, the EXCEPT operator has become available to use in this DBMS. If you are allowed to update your MySQL version, you are free to use the notation:
SELECT * FROM Tab1
EXCEPT
SELECT * FROM Tab1
WHERE int_attribute_of_Tab1>0
If Tab1 has a primary key (f.e. ID) then you could use a NOT EXISTS to itself like this :
SELECT *
FROM Tab1 AS t1
WHERE NOT EXISTS (
SELECT 1
FROM Tab1 AS t2
WHERE t2.ID = t1.ID
AND t2.int_attribute_of_Tab1 > 0
)
But it's kinda pointless in this case.
And it's not what an EXCEPT/MINUS tries to do.
(excluding identical rows)
The question's query with the EXCEPT uses the same table twice.
So reversing that WHERE criteria on Tab1 would give the same results.
SELECT *
FROM Tab1
WHERE (int_attribute_of_Tab1 <= 0 OR int_attribute_of_Tab1 IS NULL)
If it were 2 different tables then this
SELECT t1col1, t1col2, t1col3
FROM Table1
EXCEPT
SELECT t2col4, t2col5, t2col6
FROM Table2
WHERE int_attribute_of_Tab1 > 0
Could be replaced by comparing each selected column
SELECT DISTINCT t1col1, t1col2, t1col3
FROM Table1 AS t1
WHERE NOT EXISTS (
SELECT 1
FROM Table1 AS t2
WHERE t2.t2col4 = t1.t1col1
AND t2.t2col5 = t1.t1col2
AND t2.t2col6 = t1.t1col3
AND t2.int_attribute_of_Tab1 > 0
)

Delete all records from one table where they match criteria from two (or more) tables

Consider the following:
QUERY
SELECT * FROM
`table1`,`table2`
WHERE `table1`.`RemoteID` = `table2`.`ID`
AND `table2`.`UserID`=1
How can I change it from a SELECT to DELETE from table1 where these records match? It must only delete from table1, not table2
In less specific terms, I want to delete all records from table1 where they match some criteria of both tables (discretely and relatively)
You can use IN with sub query
DELETE FROM table1
WHERE `table1`.`RemoteID` IN (
SELECT ID
FROM table2
WHERE `table2`.`UserID`=1)
Try this,
Delete
from table1
where Id in
(select table1.Id
from table1 t1, table2 t2
where t1.RemoteID = t2.ID
AND table2.UserID = 1)

How to perform subquery where subquery returns more than one row?

I'm trying to search for all entries in one table where they have a column that matches entries in another column that precede a -
Example Output:
This is the query I tried, it returned the error of "Error in query (1242): Subquery returns more than 1 row"
SELECT * FROM table1
WHERE
table1.Column1 = (
SELECT
SUBSTRING_INDEX(table2.Column1,'-',1)
FROM
table2
WHERE
table2.column1 LIKE '%\-%'
);
You can use IN in your WHERE clause :
SELECT * FROM table1
WHERE
table1.Column1 IN (
SELECT
SUBSTRING_INDEX(table2.Column1,'-',1)
FROM
table2
WHERE
table2.column1 LIKE '%\-%'
);
Another way is to use JOIN as
SELECT * FROM table1 t1
inner join (
SELECT
SUBSTRING_INDEX(table2.Column1,'-',1) as str
FROM
table2
WHERE
table2.column1 LIKE '%\-%'
)t2
on
t1.column1 = t2.str
;
DEMO

Merge two queries and return one of the possible values

select value1 as value from T1 where id=10;
if id does not exist in T1 - execute another query:
select value2 as value from T2 where id=10;
So, I want to join these queries and return a single value (value1 or value2). Is it possible?
SOLUTION:
My solution:
select ifnull(value1, value2) as value from T1 left join T2 using(id) where id=10;
you can join the query using union
select value1 as value from T1 where id=10
union
select value2 as value from T2 where id=10;
as a result you can get any one of the value or both
TRY (tested)
SELECT COALESCE(t1.value1, t2.value2) AS Value FROM t1
INNER JOIN t2 USING(id)
WHERE id=10
this will always check first the table t1 for id=10, if there is no value then see table t2 for the same id
Quoted FROM
The single result column that replaces two common columns is defined
using the coalesce operation. That is, for two t1.a and t2.a the
resulting single join column a is defined as a = COALESCE(t1.a, t2.a)
You can join the two queries on the id field and then use the COALESCE function to combine the two resulting fields into the output.
This assumes that you already have a list of IDs to join against, though. Otherwise you're stuck doing a union or full join to get such a list first.
You Can use this too
select Distinct(s1.id) from sample1 as s1 inner join sample2 as s2;
use union of both
like below :
select t1.id from table1 as t1 where id=10
union
select t2.id from table2 as t2 where id=10