php Mysql get Names from 3 ID on 1 row - php

I have 2 tables
Table1 - columns:
User1Id, User2Id, User3ID
Table2 - columns:
UserID, Name, Family
How can get Name and Family from User1Id, User2Id, User3ID?

select t1.user1id, t1.user2id, t1.user3id, t1.date, t1.comment
t2.name as name1, t2.family as family1,
t3.name as name2, t3.family as family2,
t4.name as name3, t4.family as family3
from table1 t1
left join table2 t2 on t1.user1id = t2.userid
left join table2 t3 on t1.user2id = t3.userid
left join table2 t4 on t1.user3id = t4.userid

SELECT Name, Family FROM Table1, Table2 WHERE Table2.UserID = Table1.User1Id
UNION
SELECT Name, Family FROM Table1, Table2 WHERE Table2.UserID = Table1.User2Id
UNION
SELECT Name, Family FROM Table1, Table2 WHERE Table2.UserID = Table1.User3ID

Related

Regroup two SELECT

This query select all from my first table where the row id doesnt exist in my second table:
SELECT *
FROM table1
WHERE NOT EXISTS (SELECT idTable1 FROM table2 WHERE table1.id=idTable1
This query select all row of table1 who exists in the table2 with the last id of table2 ( cause one row of table1 can have multiple row of the table2 but only the last is not forget ):
SELECT *
FROM table2
INNER JOIN table 1 ON idTable1 = table1.id
WHERE table2.id IN (SELECT MAX(actioncur.id) FROM table2 GROUP BY idTable1)
I want to regroup the both query in one, I want to select all row of table1 when id doesnt exist in table2 and select all of the table1 for the last table2 id. For exemple i want to select that : row 1 -> id=44 ; table2.id= 187 ; idTable1=44. Row 2 ->id=45 ; table2.id=? ; idTable1=?
If I understand correctly, you can use a correlated subquery:
SELECT t1.*,
(SELECT MAX(t2.id)
FROM table2 t2
WHERE t1.id = t2.idTable1
) as max_t2id
FROM table1 t1;
You can also do this with LEFT JOIN and GROUP BY:
SELECT t1.*, MAX(t2.id) as max_t2id
FROM table1 t1 JOIN
table2 t2
ON t1.id = t2.idTable1
GROUP BY t1.id;

SELECT * FROM table1, table2, table3 [duplicate]

This question already has answers here:
SQL Inner-join with 3 tables?
(12 answers)
Closed 5 years ago.
I have a problem ...
In table1 I have an id, I have to compare that id in table2, then fetch the second id that is in table2 and compare it to table3 and get as a result a datum.
Example
TABLE1
ID NAME ECC...
1 Jhon
2 Frank
TABLE2
ID ID2 ECC..
1 4
2 8
TABLE3
ID NAME
4 Sea
8 Hello
If I look for id 1, the result must be Sea
If I look for id 2 the result must be Hello
Thanks!
SELECT Table3.NAME
FROM Table1
INNER JOIN Table2
ON Table1.ID = Table2.ID
INNER JOIN Table3
ON Table3.ID = Table2.ID2
WHERE Table1.ID = 1 -- Your Search here
You should use joins.
Your query will look like :
SELECT t3.name
FROM table3 t3
LEFT JOIN table2 t2 ON t3.id = t2.id2
LEFT JOIN table1 t1 ON t2.id2 = t1.id
WHERE t1.id = <your_number>
select Table2.Id,Table3.Name from Table1 inner join Table2 on Table2.ID2 = Table3.Id
Query:
SELECT a.name As Name FROM table3 a JOIN
(SELECT b.id2 AS id FROM table1 a JOIN table2 b ON a.id = b.id)b
ON a.id = b.id where b.id = <your id number (1,2) Anything>
Should be something like this:
select table3.name as name3
from table3
where table3.ID = table2.ID2
and table2.ID = table1.ID
and table1.ID = <YOURNUMBERHERE>

How to use a SELECT statement into JOIN clause?

I have a variable which is containing a dynamic query. Something like this:
$query = "SELECT id, subject FROM post1
UNION ALL
SELECT id, subject FROM post2
UNION ALL
SELECT id, subject FROM post3";
Also I have this query:
SELECT code FROM mytable WHERE id = :id
Now I want to join query above with that dynamic query. Here is my try:
SELECT t1.code t2.subject FROM mytable t1
LEFT JOIN ($query) t2 ON t1.col = t2.id
WHERE t1.id = :id
/*
SELECT t1.code t2.subject FROM mytable t1
LEFT JOIN (SELECT id, subject FROM post1
UNION ALL
SELECT id, subject FROM post2
UNION ALL
SELECT id, subject FROM post3) t2 ON t1.col = t2.id
WHERE t1.id = :id
*/
It works. But it takes a lot of time for huge data. How can I make it faster?
SELECT t1.code t2.subject FROM mytable t1
LEFT JOIN (SELECT id, subject FROM post1
JOIN mytable tt1 ON tt1.col = post1.id AND tt1.id=:id
UNION ALL
SELECT id, subject FROM post2
JOIN mytable tt2 ON tt2.col = post2.id AND tt2.id=:id
UNION ALL
SELECT id, subject FROM post3
JOIN mytable tt3 ON tt3.col = post3.id AND tt3.id=:id) t2 ON t1.col = t2.id
WHERE t1.id = :id
Just add the :id check to the internal querie4st to restrict amount of data selected.
You can use below query.
SELECT t1.code, t2.subject FROM (SELECT code, col FROM mytable WHERE id = :id ) t1
LEFT JOIN ($query) t2 ON t1.col = t2.id

Complicated MySQL Query taking forever to execute

I've a somewhat suboptimal database structure I have to work with. Table1 contains 2 Ids (lets call them id1, id2). These Ids link to 2 rows in another table Table2. I want to get some columns of Table1 and use id1 and id2 to get columns out of Table2. Do I really have to join the same table twice to link to different rows or is there another more efficient way to do this? My queries take over 30 seconds for 20 rows.
SELECT t1.id1, t1.id2, t2.name, t3.name
FROM Table1 t1, Table2 t2, Table2 t3
WHERE t1.id1 = t2.id AND t1.id2 = t3.id AND t1.index = 2
Not tested, but it should be possible to use JOINS:
SELECT t1.id1, t1.id2, t2.name FROM
FROM Table1 t1
JOIN Table2 t2
ON t1.id1 = t2.id OR t1.id2 = t2.id
WHERE t1.index = 2
If you really need this Many-to-Many relation, please try JOIN:
SELECT t2.id AS t2_id,
t3.id AS t3_id,
t2.name AS t2_name,
t3.name AS t3_name
FROM Table1 t1
JOIN Table2 t2
ON t1.id1 = t2.id
JOIN Table3 t3
ON t1.id2 = t3.id
WHERE t1.index = 2

mysql join 3 tables

How can I join three mysql tables which have one common column (id), For example, Select a, b from Table1, select c,d from table2, select e,f from table3, where id=x
Thanks
SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM table1 t1
JOIN table2 t2 ON (t1.id = t2.id)
JOIN table3 t3 ON (t1.id = t3.id)
ORDER BY t1.id;
SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM `table1` t1
JOIN `table2` t2 ON t1.id = t2.id
JOIN `table3` t3 ON t1.id = t3.id
WHERE t1.id = x
SELECT `table1`.`a`,`table2`.`c` .....
FROM `table1` JOIN `table2` USING(`id`) JOIN `table3` USING(`id`)
WHERE `id` = x
SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM table1 t1, table2 t2, table3 t3
WHERE t1.id = t2.id
AND t2.id = t3.id
AND t3.id = x
SELECT col1,col2,col3 (select any col from any table )
FROM t1 INNER JOIN t2,t3
WHERE t1.id = t2.id
AND t1.id = t3.id;
Please try this query:
SELECT product_details.product_id, product_name.pro_name,categories.cat_name
FROM product_details
INNER JOIN product_name
ON product_details.product_id=product_name.id INNER JOIN categories ON product_details.categories_id=categories.id order by product_details.id;

Categories