Use selected column in first query with IN clause in second query - mysql

Problem has been recreated below:
/* query - 1 */
Select id, title from table1;
/* returns */
id | title
-----------
1 | data-1
2 | data-2
3 | data-3
4 | data-4
5 | data-5
I want use this column id's data with IN clause in second query along with join.
Something like this:
Select id, title from table1
JOIN
Select anotherColumn from table2 where table2.id IN (1,2,3,4,5) on table1.id = table2.id
Instead of manually writing 1,2,3,4,5, how can I use the column data selected from first query in second query?
EDIT:
Actual query :
SELECT *
FROM
(
SELECT R.id, U.ic_id as rider, U.name, DP.department_name, R.location,
(R.distance - 1) + 10 as cost , R.timestamp, R.status
FROM requests AS R, iconnect.users AS U, iconnect.departments AS DP
WHERE R.pool = '125' AND R.rider = U.ic_id AND U.department = DP.id
) requestDetails
JOIN
(
SELECT AVG(rider_rating) AS rider_rating,rider
FROM
(
SELECT rider_rating, R.rider
FROM journeys AS J, requests AS R
WHERE J.req_id = R.id AND R.rider IN (12,13) LIMIT 999999
) AS allRatings
GROUP BY rider
) ratingsTable
ON requestDetails.rider = ratingsTable.rider
/* instead of (12,13) I want to use requestDetails.rider selected from the first derived table */

One option would be to use an EXISTS clause:
SELECT id, title
FROM table1 t1
WHERE EXISTS (SELECT 1 FROM table2 t2 WHERE t1.id = t2.id);
Actually, a plain inner join between the two tables would also work. But, you might want to use SELECT DISTINCT in case a given record in table1 could match more than one record in table2. That would leave us with this:
SELECT DISTINCT t1.id, t1.title
FROM table1 t1
INNER JOIN table2 t2
ON 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

skip row if user_id contains a specific code

I have these rows
user_id code
1 9103
1 9103
1 9001
2 9103
3 9103
3 9104
4 9103
4 9103
4 9001
I want to get only id that not contains 9001, then only 2 and 3
I try with Distinct But I without lucky
Select distinct v.code, user_id from mytable as v
where v.code not in ( Select v2.code from mytable as v2
where v2.code=9001)
Group by the user and then take only those groups having no record of the condition
select user_id
from your_table
group by user_id
having sum(code = 9001) = 0
There are multiple methods to get the results you need.
NOT EXISTS (ALL DBMS)
SELECT
*
FROM
Table1
WHERE
NOT EXISTS (
SELECT
1
FROM
Table1
WHERE
code = 9001
)
NOT IN (ALL DBMS)
SELECT
DISTINCT
Table1.user_id
FROM
Table1
WHERE
user_id NOT IN (
SELECT
user_id
FROM
Table1
WHERE
code = 9001
)
RIGHT JOIN / LEFT JOIN (ALL DBMS but for example SQLite does not support RIGHT JOIN)
SELECT
DISTINCT
Table1.user_id
FROM (
SELECT
user_id
FROM
Table1
WHERE
code = 9001
) AS Table1_filter
RIGHT JOIN
Table1
ON
Table1_filter.user_id = Table1.user_id
WHERE
Table1_filter.user_id IS NULL
;
SELECT
DISTINCT
Table1.user_id
FROM
Table1
LEFT JOIN (
SELECT
user_id
FROM
Table1
WHERE
code = 9001
) AS Table1_filter
ON
Table1_filter.user_id = Table1.user_id
WHERE
Table1_filter.user_id IS NULL
;
Conditional SUM (#juergen d answer) (ALL DBMS)
SELECT
Table1.user_id
FROM
Table1
GROUP BY
Table1.user_id
HAVING
SUM(Table1.code = 9001) = 0
Variation on (#juergen d answer) with GROUP_CONCAT (MySQL and SQLite only)
Also possible with
... HAVING FIND_IN_SET('9001', GROUP_CONCAT(Table1.code)) = 0 (MySQL Only)
SELECT
Table1.user_id
FROM
Table1
GROUP BY
Table1.user_id
HAVING
GROUP_CONCAT(Table1.code) NOT LIKE '%9001%'
p.s GROUP_CONCAT(Table1.code) NOT LIKE '%9001%' might also select false positives depending on the data used. Using FIND_IN_SET('9001', GROUP_CONCAT(Table1.code)) = 0 is more safe option to use.
see demo http://sqlfiddle.com/#!9/fc6f6b/34

get data from two tables fetching only one row from second table

i have two tables
t1:
sno val
12 A
13 B
14 C
t2:
refid val2
13 ba
13 bb
13 bc
using the following query:
select * from t1,t2 where t1.sno=t2.refid order by t2.refid;
and the query
select * from t1 join t2 on t2.refid = ( select refid from t2 where t1.sno = t2.refid limit 1 )
I get
B ba
B bb
B bc
and so on for all the possible combinations.
I want to get only first combination instead of all three like
B ba
C ca
and so on...
I mean, I just want only first row for every three combination that I am getting as a result of join.
As per my knowledge, I suggest below two ways to get your required output:
Way1 :
Use nested query to group by records then do join
select * from t1 a join (select * from t2 group by refid) b on a.sno= b.refid
way2:
But it's better to add auto increment key for tables like t2. Once you added auto increment key that will be useful in most use cases.
First add auto increment key to your table t2 by using below command
ALTER TABLE t2 add column id int(11) key auto_increment first;
Then execute the below join query:
select * from t1 a join (select *from t2 where t2.id in(select min(id) from t2 group by refid)) b on a.sno= b.refid
select t1.val,temp.refid from t1,(select t2.refid as refid, min(t2.val2) as val2 /*Assuming "first" row criteria*/ from t2 group by t2.refid) temp where t1.sno=temp.refid order by t2.sno;
Eliminated duplicates from t2 using sub-query and joined with t1.
If you want only one value and one column from the second table, a correlated subquery is an easy solution:
select t1.*,
(select t2.val2
from t2
where t1.sno = t2.refid
order by ? -- whatever defines "first"
limit 1
) as val2
from t1
order by t1.sno;
If you only want rows where there is a match, MySQL has a nice extension for having that fits the bill:
select t1.*,
(select t2.val2
from t2
where t1.sno = t2.refid
order by ?-- whatever defines "first"
limit 1
) as val2
from t1
having val2 is not null
order by t1.sno;
select * from (select ROW_NUMBER() OVER (PARTITION BY sno ORDER BY val2 ) AS rn, * from [dbo].[t1] a
join [dbo].[t2] b on a.sno = b.refid
) t
where rn = 1

need help on join query condtion

I am not much in db queries and I require some help on following .
I have two table structures as follow as
table1 :
Name Id1 Id2
Jack 1 1
Jack 1 1
Jack 1 1
table2 :
Name Id1 Id2
Jack 1 1
I used basic join query :
select *
from table1 tb1
join table2 tb2 on tb1.id1 = tb2.id1
and tb2.id2 = tb2.id2
output I get :
Jack 1 1
Jack 1 1
Jack 1 1
But I required following output:
Jack 1 1
Note: I like to show what records is available in table 2 when it combined together!!.. I would like to fetch data with respect table2 only
Thanks in advance.
Please try the following (does not uses aliases) ...
SELECT table2.Name,
table2.Id1,
table2.Id2
FROM table1
JOIN table2 ON table1.id1 = table2.id1 AND
table1.id2 = table2.id2
GROUP BY table2.id1,
table2.id2;
Or try the following (does use aliases)...
SELECT tb2.Name,
tb2.Id1,
tb2.Id2
FROM table1 tb1
JOIN table2 tb2 ON tb1.id1 = tb2.id1 AND
tb1.id2 = tb2.id2
GROUP BY tb2.id1,
tb2.id2;
By performing an INNER JOIN on table2 with table1 on those key fields you are limiting the output to only those rows from table1 that match Id1 and Id2 in table2 (Note : Where JOIN is not preceded by a JOIN type an INNER JOIN is performed). Since multiple rows in table1 meet this criteria you can limit your results to just one row for each matching set of criteria using GROUP BY
This should change the supplied actual query to...
SELECT delta.input_name,
delta.mtcn,
delta.at‌​tempt_id
FROM compliance.rtra_transactions rtra_txn
JOIN compliance.GNR_TEST_RUNS delta ON rtra_txn.mtcn_nr = delta.mtcn
AND rtra_txn.attemptid = delta.attempt_id
WHERE rtra_txn.year = 2017
AND rtra_txn.month = 2
AND rtra_txn.day = 17
AND rtra_txn.trns_ts BETWEEN '2017-02-17 00:00:00' AND '2017-02-17 23:59:00'
AND delta.MATCH_OUTCOME = 'MATCH'
AND delta.job_name = 'Feb17_Run_1'
AND rtra_txn.txn_map[ 'TRANSACTIONTYPE' ] IN ( '10', '7' )
GROUP BY delta.mtcn,
delta.attempt_id;
If you have any questions or comments, then please feel free to post a Comment accordingly.
Following query should work :
SELECT * FROM TBL2 T2
UNION
SELECT * FROM TBL1 T1;
You need to use LEFT JOIN as you mentioned in Biswabid answer comment I like to show what records is available in table 2 alone
select DISTINCT tb2.*
from table2 tb2
left join table1 tb1 on tb1.id1 = tb2.id1
and tb1.id2 = tb2.id2
WHERE tb1.id1 IS NULL

how to use inner join in my condition

I have two tables like these
c_ID name
--- ------
7 a
6 a
5 a
4 d
AND
c_ID photo
----- ------
7 1
6 1
5 0
4 1
How can i select records that name is a and photo is 1 ?
Thanks
select * /*TODO: Add specific column(s) you want here*/
from table1
join table2
on table1.c_ID = table2.c_ID
where table1.name = 'a'
and table2.photo = 1
SELECT t1.*, t2.*
FROM table1 t1
JOIN table2 t2 ON t2.c_ID = t1.c_ID
WHERE t1.name = 'a' AND t2.photo = 1
It is good practice not to use upper case characters in databases.
SELECT *
FROM table1 AS name LEFT JOIN table2 AS photo ON name.c_ID = photo.c_ID
WHERE name.name = 'a' and photo.photo = 1
This being said, the way your example looks maybe you could normalize your two tables into one table with the fields c_ID, name and photo
SELECT table1.c_ID, table1.name, table2.photo // desired fields
FROM table1 INNER JOIN table2 ON table1.c_ID=table2.c_ID // joining tables on common keys
WHERE table1.name='a' AND table2.photo=1; // desired condition
Try this:
SELECT table1.c_id, table1.name, table2.photo
FROM table1 INNER JOIN table2
ON table1.c_id = table2.c_id
AND table1.name = 'a'
AND table2.photo = 1