how to use inner join in my condition - mysql

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

Related

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

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;

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 make query show the an other column of the primary key?

umm I'm not sure I've made the title right but its kind of hard to express it in short words.
I have to tables
table1:
id | name
1 | alice
2 | bob
table 2:
user_id | date
2 | 2014-11-1
2 | 2014-11-2
1 | 2014-11-3
as a query, if I want to show table 2 but instead of the integer numbers of user_id, I want it to show the corresponding names of the users where this info is stored in table 1.
I think this is supposed to be easy but I don't know how to get this done.
A query along the lines of -
select t1.name, t2.date
from table_1 t1 inner join table_2 t2 on t1.id = t2.user_id
Try:
SELECT t2.user_id, t1.name
FROM table1 t1 INNER JOIN table2 t2
ON t1.id = t2.user_id
This will do it.
SELECT
`b`.`name`,
`a`.`date`
FROM
table2 a
INNER JOIN table1 b ON (a.user_id = b.id)
SELECT
B.[Name]
,A.[date]
FROM [table 2] A
LEFT OUTER JOIN [table1] B
ON A.[user_id] = B.[id]

MySQL SELECT all from first table and join matched data from second table with specific where clause for second table filter

I have an issue when trying to fetch out my data from my database.
here are my table design
I have an issue when trying to fetch out my data from my database.
here are my table design
table1:
user_id username
1 test
2 test2
3 test3
table2:
id table2_userid key value
1 2 position admin
2 2 name myname
What i want to output is:
user_id username key value
1 test NULL NULL
2 test2 position admin
3 test3 NULL NULL
This is my current sql code:
SELECT table1.user_id, table1.username, table2.key, table2.value
FROM table1
LEFT JOIN table2 ON table1.user_id = table2.table2_userid WHERE table2.key="position"
However, this return nothing. Please help me in this.
Thanks.
Try following Query it will work for your problem:
SELECT table1.user_id, table1.username, table2.key, table2.value FROM
table1 LEFT JOIN table2 ON table1.user_id = table2.table2_userid and
table2.key="position" group by table1.user_id
Try using single quotes:
SELECT table1.user_id, table1.username, table2.key, table2.value
FROM table1
LEFT JOIN table2 ON table1.user_id = table2.table2_userid WHERE table2.key = 'position'
Otherwise your query seems fine to me.
I've read this on SO in a comment: [S]ingle for [S]trings; [D]ouble for [D]atabase
Move your condition from where clause to on clause
SELECT
t1.user_id,
t1.username,
t2.key,
t2.value
FROM table1 t1
LEFT JOIN table2 t2
ON t1.user_id = t2.table2_userid
AND t2.key="position"
Demo

How to get value from two table if id is same in sql

I am very new in sql, then i am so confused how to get join or get value from two.
First table:
ID P_ID Name AGE U_ID
1 5 B 8 5w
2 8 D 17 6j
3 7 R 67 0qw
Second Table:
ID P_ID Address Edu
1 6 Bddd +2
2 7 Dssss Bachelor
3 2 rress Phd
Here, i want to get accorading to P_ID, but i have U_ID only.
For this: Let us assume that now I have U_ID=0qw.
How to get value from second table. Address and edu , and Age Thanks in advance.
Join on the column that both tables have in common.
select t1.age, t2.address, t2.edu
from table1 t1
join table2 t2 on t1.p_id = t2.p_id
where t1.u_id = '0qw'
Then use the table names or alias names (like t1 for table1) to pick columns from the tables you join.
I think you are looking forward to this:
SELECT t2.Address, t2.Edu, t1.Age
FROM firstTable t1
JOIN secondTable t2
ON t1.P_ID = t2.P_ID
WHERE t1.U_ID = '0qw'
SELECT table1.AGE
, table2.Address
, table2.Edu
FROM table1
INNER JOIN table2 ON (table1.P_ID = table2.P_ID)
WHERE table1.U_ID = '0qw';
NOTE: SQL query is not case sensitive.