Fetch result based on result of another query - mysql

How can I fetch a result from a database, based on the results of another query, in a single query?
Ie:
Fetch -> Record -> Where ID equals -> (Fetch -> Field -> Where ID equals $variable)
Does that make sense?

something like this:
select * from tableA where cloInAB in (select colInAB from tableB where colB = 5);

I think a join would be better than a query on the result of a subquery - something like:
select A.* from tableA A
join tableB B on A.colInAB = B.colInAB
where B.colB = 5

Related

Mysql query with if exists check in where clause

I have two tables (A and B) where my query compares a calculation from table A with a range in table B and then insert a corresponding value to the range(also in table B) in the third table(table C) based on dates. However,it is a possibility that table A may not have data for everyday and for those days i want to enter the value against the second lowest range.
TABLE B
id(PK)|date| v1 | v2
TABLE A
Aid|id(FK)|MinRange|MaxRange|Value
TABLE C
Cid|b.date|id(FK)|b.v1|b.v2|A.value
I am looking for a way to embed IF EXISTS in the WHERE clause something like this:
SELECT B.value
from TableB B
INNER JOIN TableA A ON A.id=B.id
WHERE B.id=4
and (IF DATA EXISTS) B.v1+B.v2 between A.min and A.max (ELSE Choose the second lowest A.min)`
The query above is an example to explain what i am trying to do, hence, it is not a valid query. I do not want to use a subquery for obvious performance issues.
I will appreciate any help.Thanks in advance :)
What about this
UPDATE
DECLARE myvar INT;
SELECT COUNT(*) INTO myvar FROM TableB WHERE Date=p_date;
SELECT myvar;
IF(myvar>0)
SELECT B.Date,A.Value
from TableB B
left JOIN TableA A ON A.id=B.id
WHERE B.id=4
ELSE
SELECT p_date AS Date,predefinedvalue AS Value
END IF;
You have to use left join because inner join will retrieve only the matching records. Also the predefined value has to be stored in B since if there is no matching record you can't query anything from A.
SELECT CASE WHEN myvar>0 THEN B.Date
ELSE p_date
END AS Date,
CASE WHEN myvar>0 THEN A.Value
ELSE predefinedvalue
END AS Value
from TableB B
left JOIN TableA A ON A.id=B.id
WHERE B.id=4

Select results from one query to another

SELECT id FROM table1 WHERE name LIKE 'test%';
That would show me all the ids in table1 with the id of anything that matched test%. So I have been doing this the long way doing this :
SELECT * FROM table2 WHERE id = '1011';
Is there anyway to make my table1 query jump and auto insert to WHERE id = '1011', I'd like it to auto match it up whats in query one to query two. Instead of having to run the second query over and over again and get all the results faster.
You want to do a JOIN:
SELECT a.*
FROM table2 a
JOIN table1 b
ON a.id = b.id
WHERE b.name LIKE 'test%'

SQL - store a query as a local variable

So I need to do a query based on the results of another query. Is there a way for me to set the result of a query to a variable so I can use the result of that in another query?
Example:
{original query} --->
SELECT
tablea.number
FROM tablea
INNER JOIN tableb
ON tablea.name = tableb.name
WHERE tableb.zip = '11111'
Now I want to use this result to then do another query:
SELECT *
FROM tablea
WHERE number = (results of last query)
I think theres a way to set the first query to a variable so that that way the second one can just use the result, just not sure how. Thanks.
You can use IN or EXISTS:
SELECT *
FROM tablea
WHERE name IN (SELECT name FROM tableb WHERE zip = '11111')
Or
SELECT *
FROM tablea A
WHERE EXISTS (SELECT 1 FROM tableb
WHERE zip = '11111'
AND name = A.name)
You can select directly into a variable:
declare #number int
select #number = tablea.number
from ...etc...
select *
from tablea
where number = #number
There are a few other ways of doing the same thing, but this is how you'd do it with a variable.

MySQL multiple SELECT statement count results returned per table

I'm trying to select 3 tables at once for a query. I would like to know what is the best way to determine how many rows are returned for each table?
Doing this individually, I have
SELECT * from tableA
SELECT * from tableB
SELECT * from tableC
If I do it this way, I can see on each select how many rows are returned. I would like to select all these at once which I've done successfully, but I'd like to know how to pick up results per table returned. Example query below:
SELECT * from tableA ta WHERE id=100
SELECT * from tableB tb where pid=100
SELECT * from tableC tc where cid = 100
Is it just a matter of doing this?
SELECT (count(id) from tableA where id=100) as count_tableA,
SELECT * from tableA ta WHERE id=100,
SELECT (count(pid) from tableB where id=100) as count_tableB,
SELECT * from tableB tb where pid=100,
SELECT (count(id) from tableB where cid=100) as count_tableC,
SELECT * from tableC tc where cid = 100
The overall goal is to increase performance by avoiding 3 queries each time, but with that, I need to isolate how many rows to pick up from each table that is returned.
Well, you can't really avoid the queries. You have to query each table to get the rows, and that requires a full-table scan.
You can optimize the counts by creating indexes on tableA(id), tableB(id), and tableC(cid).
You can also fetch the rows in your application layer, and then do the count afterwards.
The syntax for your query is not correct. Perhaps you mean:
select (SELECT count(id) from tableA where id=100) as count_tableA, a.*
from TableA
where id = 100;
And so on.

MySQL Subquery Optimization

The Query:
SELECT a FROM table1 WHERE b IN (SELECT b FROM table1 WHERE c = 2);
If the subquery returns zero results, mysql takes a long time to realize it before it finally returns an empty result. (2.5s when subquery is empty, 0.0005s when there is a subquery result).
My question: is there a way to modify the query such that it will still return an empty result but take the same time as it did when there was a result?
I tried:
SELECT a FROM table1 WHERE b IN ((SELECT b FROM table1 WHERE c = 2), 555);
...but it only works WHEN the subquery is empty. If there is a result, the query fails.
-- I don't want to change the query format from nested to join/etc.
Ideas..? Thanks!
-- EDIT: Also, I forgot to add: The subquery will likely result in a decent-sized list of results, not just one result. --- ALSO, when I type '555', I am referring to a value that will never exist in the column.
-- EDIT 2: I also tried the following query and it "works" but it still takes several orders of magnitude longer than the original query when it has results:
SELECT a FROM table1 WHERE b IN (SELECT 555 AS b UNION SELECT b FROM table1 WHERE c = 2);
Wild guess (I can't test it right now):
SELECT a FROM table1 WHERE
EXISTS (SELECT b FROM table1 WHERE c = 2)
AND b IN (SELECT b FROM table1 WHERE c = 2);