inner join for a query? - mysql

I want to do a sql query and have some problems:
I want to select from table_1 the ID's Where parent_id is the value I have:
SELECT ID
FROM table_1
WHERE parent_ID = 'x'
I want to use the ID'S I got in 1. and
SELECT
FROM table_2
WHERE ID = 'The ID's from Query 1.'

Like this?
select ...
from table_1 a
join table_2 b on(a.id = b.id)
where a.parent_id = 'x';
Edit
Note: the query will potentially produce duplicate rows depending on the keys and relation between the tables. For example, you will get duplicates if, for a given table_1.parent_id = X, there can be multiple occurrences of the same table_1.ID.
Another example is when table_2.ID isn't unique.
In those cases you would want to remove the duplicates (using distinct, group by, partitioned #row_number, etc) or, not produce the duplicates in the first place using a semi-join instead (exists, in). Have a look #OMG Ponies answer for reference.

Using IN
SELECT t2.*
FROM TABLE_2 t2
WHERE t2.id IN (SELECT t1.id
FROM TABLE_1 t1
WHERE t1.parent_id = 'x')
Using EXISTS
SELECT t2.*
FROM TABLE_2 t2
WHERE EXISTS (SELECT NULL
FROM TABLE_1 t1
WHERE t1.id = t2.id
AND t1.parent_id = 'x')
Using an INNER JOIN
The DISTINCT (or GROUP BY) is necessary to eliminate duplicates if there are more than one records in TABLE_1 that relate to a record in TABLE_2:
SELECT DISTINCT t2.*
FROM TABLE_2 t2
JOIN TABLE_1 t1 ON t1.id = t2.id
AND t1.parent_id = 'x'

It can be solved with the use of IN as follows:
SELECT * FROM table_2 WHERE ID IN (SELECT ID FROM table_1 WHERE parent_ID = 'x')

select * from table_2 where id in (select id from table_1 where parent_id = 'x')

Yes, it's better to you use this:
SELECT [value]
FROM [table2]
WHERE [value] IN (SELECT [value]
FROM [table1]
WHERE [value] = "[value]"
)

Related

how to return all the fields of table2 based upon the occurrence of the id in the table1

I have 2 tables, one is table1
and another is table 2
I want the result by a query, like
I have tried select id from table2 order by (select id from table1); but it is giving error.
You can join and sort. But you need a column that defines the ordering of the rows in table1. Let me assume that you have such column, and that is is called ordering_id.
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id
order by t1.ordering_id
You can even use a subquery in the order by clause:
select *
from table2 t2
order by (select t1.ordering_id from table1 t1 where t1.id = t2.id)
Join the two tables and then order the result.But for that you need to have some column for ordering and this does not seems to be the case. Syntax you are using for ordering will not work.
SELECT A.ID, B.NAME FROM TABLE1 A INNER JOIN TABLE2 B
ON(A.ID = B.ID) ORDER BY A.ID DESC
finally got the answer
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id;

SQL: full outer join (ambitious column name)

I have two table, t1 and t2.
-- t1
id name address
1 Tim A
2 Marta B
-- t2
id name address
1 Tim A
3 Katarina C
If I do t1 full outer join with t2
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION ALL
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
However, the result has ambitious id, name, address.
How do I rename this so that I don't have duplicate column name?
Attempt:
SELECT name, address FROM
(SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION ALL
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id) as derived_table;
return: ERROR- duplicate column name "name".
Ditch the * in the SELECT list.
Specify the list of expressions to be returned. And qualify all column references with either the table name, or preferably, a shorter table alias.
And assign an alias to the expression and that will be the name of the column in the resultset.
Also, the query shown is not equivalent to a FULL OUTER JOIN.
If the goal is return all rows from t1, and to also return rows from t2 where a matching row doesn't exist in t1, I'd do something like this...
SELECT t.id AS t_id
, t.name AS t_name
, t.addr AS t_addr
FROM t1 t
UNION ALL
SELECT s.id
, s.name
, s.addr
FROM t2 s
LEFT
JOIN t1 r
ON r.id = s.id
WHERE r.id IS NULL
Try fully qualifying it like
SELECT t1.id, t1.name, t1.address FROM t1

Can a table get other table column data

I need to create a select statement where the statement need to retrieve data from other table column data
eg.
Table1 Table2
id id2
age age2
Select id, age from table 1 where id= id2
Is that possible.
You can use INNER JOIN
SELECT
T1.id,
T1.age
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.id = T2.id2
DEMO using INNER JOIN
You can use EXISTS
SELECT
T1.id,
T1.age
FROM Table1 AS T1
WHERE EXISTS(
SELECT 1
FROM Table2 AS T2
WHERE T2.id2 = T1.id
);
You can use IN
SELECT
T1.id,
T1.age
FROM Table1 AS T1
WHERE T1.id IN (SELECT T2.id2 FROM Table2 AS T2)
Note:
In the working demo the output consists of two rows. There are two entries in tabel1 and three entries in table2. But there are only two matching entries found between these two tables. That's why output consists of only two rows.
Yes you can. It is called a JOIN and there are several types of JOINs. I suggest you read up on them on SQL JOINs.
SELECT id ,age
FROM TABLE 1
WHERE id IN (SELECT id2 FROM TABLE2);
OR
SELECT id ,age
FROM TABLE1 , TABLE2
WHERE id = id2 ;
OR
SELECT id ,age
FROM TABLE 1 , (SELECT id2 FROM TABLE2) TBL2
WHERE id = TBL2.id2 ;

SQL query to find unreferenced records

I have two tables bound through an ID field:
table1: id, name, type
table2: id, id_table1, date, status
I have to collect all the records of the table1 that have a certain value of type field and that are not been referenced in table2 plus all the records of table1 referenced in table2 that have a certain status field value.
For the first part if I remember correctly I can use the LEFT JOIN command:
LEFT JOIN table1.name
LEFT JOIN table2
ON table2.id_table1 = table1.id
WHERE (table1.value = 'value1') AND (table2.id_table1 IS NULL);
but for the second part I'm getting lost...
I'm using MySQL 5.6 and I would like to define a View to handle this.
SELECT t1.*, t2.*
FROM table1 t1
LEFT JOIN table2 t2
ON table2.id_table1 = table1.id
WHERE (t1.type= 'value1' AND t2.id IS NULL)
OR (t2.status = 'certain status' )
I would think you could just change the WHERE to:
WHERE (table1.value = 'value1')
AND (table2.id_table1 IS NULL
OR
([the other table2 status criteria)
)
;
You can try this...
SELECT T1.*,T2.*
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T1.Value = 'value1' AND T2.id_table1 IS NULL
UNION
SELECT T1.*,T2.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T2.Status= 'Status Criteria'

use a mysql query result to do another query

I have 2 table table1 contains an userid and a postid. Table2 contains a userid and a username. I want to return all the userids with a certain postid and then use those userids to query table2 and get the usernames.
Is there a way to go about this? I have tried join statement and it doesn't seem to work
Use the IN function:
select username, userid from `table2` where userid in (select userid from `table` where postid = <condition>)
SELECT A.postid, A.userid, B.username FROM
tableA AS A JOIN tableB AS B ON A.userid=B.userid
select post id, user id from tableA username from tableB. Join these 2 different table columns when tableA's userid is equal to tableB's userid.
select t1.postid, t1.userid, t2.username
from table1 t2
left join table1 t2 on t1.userid = t2.userid
where t1.postid = <your postid>
Join the tables on the common columns
select t1.postid, t1.userid, t2.username
from table1 t1
join table2 t2 on t1.userid = t2.userid
where t1.postid = 2