use a mysql query result to do another query - mysql

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

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 ;

One SQL statement for two Foreign Keys

I have two tables. The first table contains ID, First_Name and Last_Name.
The 2nd table contains two foreign key fields containing different ID's of the first table.
I want to be able to run a SQL query that gets reults of the 2nd table which then grabs the First_Name of each member based on the two different foreign keys.
How would I go about doing this?
select t2.*, t1a.firstname, t1b.firstname
from table2 t2
left join table1 t1a on t2.fk1 = t1a.id
left join table1 t1b on t2.fk2 = t1b.id
Suppose the second table has fields as such
userid, supervisorid ( both referring to the Id column of the first table )
you may write join to get the value like this
SELECT t2.*, ID, firstname, lastname FROM table 2 t2
LEFT OUTER JOIN table 1 t1 ON
t2.userid = t1.id
OR t2.supervisorid = t1.id
I think correct sql would be below one using OR condition in outer join or using union
SELECT t1.id,t1.name from table1 t1, table2 t2 WHERE t1.id1 = t2.id1
UNION
SELECT t1.id,t1.name from table1 t1, table2 t2 WHERE t1.id1 = t2.id0
SELECT t1.id, t1.name from table2 t2 LEFT OUTER JOIN table1 t2 ON t1.id = t2.id or t1.id1 = t2.id0

inner join for a query?

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]"
)