How to join 3 table in mysql with rule? - mysql

First, I have 3 table.
Table 1 data:
tid | type_1 | address_1 | contact_1
----+--------+-----------+-----------+
1 | 4 | No.2123 | 01234567
4 | 4 | No.4567 | 00011234
Table 2 data:
tid | type_2 | address_2 | contact_2
----+--------+-----------+-----------+
2 | 3 | No.8888 | 7654321
Table 3 data:
tid | subject | desc
----+---------+-------------+
1 | Test 1 | Desc 1
2 | Test 2 | Desc 2
3 | Test 3 | Desc 3
4 | Test 4 | Desc 4
I would like to combine like this:
tid | subject | type_1 | type_2 | address_1 | address_2 | contact_1 | contact_2 | desc
----+---------+--------+--------+-----------+-----------+-----------+-----------+------
1 | Test 1 | 4 | | No.2123 | |01234567 | | Desc1
2 | Test 2 | | 3 | |No.8888 | |7654321 | Desc2
4 | Test 4 | 4 | | No.4567 | |00011234 | | Desc4
if table 1 got "tid" 1, then table 2 never got the same tid.
Ignore if tid no found in table 1 and table 2
Thanks!
My code before is:
$query = mysql_query("SELECT table1.*, table2.*, table3.subject, table3.desc, FROM table1 ON table1.tid = table3.tid LEFT JOIN table2 ON table2.tid = table3.tid LEFT JOIN table3 ON table1.tid AND table2.tid = table3.tid");

SELECT
table1.tid,
table3.subject,
table1.type_1,
NULL AS type_2,
table1.address_1,
NULL AS address_2,
table1.contact_1,
NULL AS contact_2,
table3.`desc`
FROM table1
INNER JOIN table3 ON table1.tid=table3.tid
UNION ALL
SELECT
table2.tid,
table3.subject,
NULL AS type_1,
table2.type_2,
NULL AS address_1,
table2.address_2,
NULL AS contact_1,
table2.contact_2,
table3.`desc`
FROM table2
INNER JOIN table3 ON table2.tid=table3.tid

DROP TABLE IF EXISTS table1;
CREATE TABLE table1
(tid INT NOT NULL
,type_1 INT NOT NULL
,address_1 VARCHAR(12) NOT NULL
,contact_1 VARCHAR(12) NOT NULL
);
INSERT INTO table1 VALUES
(1 ,4 ,'No.2123','01234567'),
(4 ,4 ,'No.4567','00011234');
DROP TABLE IF EXISTS table2;
CREATE TABLE table2
(tid INT NOT NULL
,type_2 INT NOT NULL
,address_2 VARCHAR(12) NOT NULL
,contact_2 VARCHAR(12) NOT NULL
);
INSERT INTO table2 VALUES
(2 ,3 ,'No.8888','7654321');
DROP TABLE IF EXISTS table3;
CREATE TABLE table3
(tid INT NOT NULL
,subject VARCHAR(12) NOT NULL
,description VARCHAR(12) NOT NULL
);
INSERT INTO table3 VALUES
(1 ,'Test 1','Desc 1'),
(2 ,'Test 2','Desc 2'),
(3 ,'Test 3','Desc 3'),
(4 ,'Test 4','Desc 4');
SELECT x.*
, y.subject
, y.description
FROM
( SELECT tid
, type_1
, NULL type_2
, address_1
, contact_1
, NULL address_2
, NULL contact_2
FROM table1
UNION
SELECT tid
, NULL
, type_2
, NULL
, NULL
, address_2
, contact_2
FROM table2
) x
JOIN table3 y
ON y.tid = x.tid;
+-----+--------+--------+-----------+-----------+-----------+-----------+---------+-------------+
| tid | type_1 | type_2 | address_1 | contact_1 | address_2 | contact_2 | subject | description |
+-----+--------+--------+-----------+-----------+-----------+-----------+---------+-------------+
| 1 | 4 | NULL | No.2123 | 01234567 | NULL | NULL | Test 1 | Desc 1 |
| 2 | NULL | 3 | NULL | NULL | No.8888 | 7654321 | Test 2 | Desc 2 |
| 4 | 4 | NULL | No.4567 | 00011234 | NULL | NULL | Test 4 | Desc 4 |
+-----+--------+--------+-----------+-----------+-----------+-----------+---------+-------------+
3 rows in set (0.00 sec)

The JOIN clause you have written is not in correct format. You should use it like this:
`table1 JOIN table2 ON` your conditions to join the table.
You can get more about JOIN from here.
So you can try this:
SELECT table3.tid AS tid,
t3.subject AS subject,
t1.type_1 AS type_1,
t2.type_2 AS type_2,
t1.address_1 AS address_1,
t2.address_2 AS address_2,
t1.contact_1 AS contact_1,
t2.contact_2 AS contact_2,
t3.desc AS description
FROM table3 t3
INNER JOIN table1 t1 ON t3.tid = t1.tid
INNER JOIN table2 t2 ON t3.tid = t2.tid

Related

Get result from joined tables

I have 2 tables:
Table 1:
| jobid | jobname |
| 1 | job a |
| 2 | job b |
Table 2:
| id | jobid | statusid | statusdate | desc |
| 1 | 1 | 100 | 2019.04.25 10:00:00 | first |
| 2 | 2 | 100 | 2019.04.25 11:00:00 | first |
| 3 | 2 | 100 | 2019.04.25 12:00:00 | second |
Jobs in table2 can have more then one same "statusid", but different "statusdate" and "desc"
I need to get jobs list with the last "statusid" = 100 like this :
| 1 | job a | 1 | 1 | 100 | 2019.04.25 10:00:00 | first |
| 2 | job b | 3 | 2 | 100 | 2019.04.25 12:00:00 | second |
SELECT * FROM table1
INNER JOIN table2 ON table1.id = table2.jobid
GROUP BY table1.id
This query return wrong result like:
| 1 | job a | 1 | 1 | | 100 | 2019.04.25 10:00:00 | first |
| 2 | job b | 3 | 2 | 2 | 100 | 2019.04.25 11:00:00 | first |
You should be able to accomplish that by doing something like this:
Table
drop table if exists table1;
create table table1 (jobid int, jobname char(10));
insert into table1 values (1, 'job a'), (2, 'job b');
drop table if exists table2;
create table table2 (
id int,
jobid int,
statusid int,
statusdate timestamp,
`desc` char(10)
);
insert into table2 values
(1,1,100,'2019.04.25 10:00:00','first')
,(2,2,100,'2019.04.25 11:00:00','first')
,(3,2,100,'2019.04.25 12:00:00','second');
Query
select
t1.*,
t2.*
from table1 t1
inner join (
select jobid, max(statusdate) as maxstatusdate
from table2
group by jobid
) tn on t1.jobid = tn.jobid
inner join table2 t2 on tn.jobid = t2.jobid and tn.maxstatusdate = t2.statusdate;
Results
jobid jobname id jobid statusid statusdate desc
1 job a 1 1 100 25.04.2019 10:00:00 first
2 job b 3 2 100 25.04.2019 12:00:00 second
Explanation
For each job ID, find the maximum status date
Join that to table1 to get information from table1. Common field is jobid
Join their result to table2 that has all the remaining information you want. Common fields are jobid and statusdate. Since we aliased max status date to a different name, make sure we are using the correct name in the join
Example: https://rextester.com/HRSWZ89705
DROP TABLE IF EXISTS table1;
CREATE TABLE table1
(jobid INT NOT NULL PRIMARY KEY
,jobname VARCHAR(12) UNIQUE
);
INSERT INTO table1 VALUES
(1,'job a'),
(2,'job b'),
(3,'job c');
DROP TABLE IF EXISTS table2;
CREATE TABLE table2
(id SERIAL PRIMARY KEY
,jobid INT NOT NULL
,statusid INT NOT NULL
,statusdate DATETIME NOT NULL
,description VARCHAR(12) NOT NULL
);
INSERT INTO table2 VALUES
(1,1,100,'2019-04-25 10:00:00','first'),
(2,2,100,'2019-04-25 11:00:00','first'),
(3,2,100,'2019-04-25 12:00:00','second');
SELECT a.*
, b.id x_id
, b.statusid
, b.statusdate
, b.description
FROM table1 a
LEFT
JOIN
( SELECT x.*
FROM table2 x
JOIN
( SELECT MAX(id) id
FROM table2
WHERE statusid = 100
GROUP
BY jobid
) y
ON y.id = x.id
) b
ON b.jobid = a.jobid
;
+-------+---------+------+----------+---------------------+-------------+
| jobid | jobname | x_id | statusid | statusdate | description |
+-------+---------+------+----------+---------------------+-------------+
| 1 | job a | 1 | 100 | 2019-04-25 10:00:00 | first |
| 2 | job b | 3 | 100 | 2019-04-25 12:00:00 | second |
| 3 | job c | NULL | NULL | NULL | NULL |
+-------+---------+------+----------+---------------------+-------------+
SELECT
t1.*,t2.* FROM
(SELECT
MAX(id) as id
FROM
table2
WHERE statusid = 100
GROUP BY jobid) AS f
JOIN table2 t2
ON t2.id = f.id
JOIN table1 t1
ON t2.jobid = t1.jobid
The sub query select finds the last id for a row with statusid 100, then it joins the actual table based on this id.
You can reorder this as you wish using the correct joins.

Select row by column id AND include rows that contain the same value from another column if null

I am trying to get a row by ID but also include rows that have the same value in other column but not including them if the value is null or empty.
Data:
+------+------+------+
|ID GROUP |AREA |
+------+------+------+
| 1 | A | AA |
+------+------+------+
| 2 | | AA |
+------+------+------+
| 3 | A | AA |
+------+------+------+
| 4 | B | AA |
+------+------+------+
| 5 | | BB |
+------+------+------+
| 6 | A | AA |
+------+------+------+
| 7 | B | BB |
+------+------+------+
| 8 | | AA |
+------+------+------+
What I have now:
SELECT * WHERE ID = 1 AND AREA = "AA"
Which returns:
+------+------+------+
| 1 | A | AA |
+------+------+------+
But I also want to get all the rows that contain GROUP "A":
+------+------+------+
| 1 | A | AA |
+------+------+------+
| 3 | A | AA |
+------+------+------+
| 6 | A | AA |
+------+------+------+
But would need to return just the ID requested if the "GROUP" column is Null.
SELECT * WHERE ID = 2 AND AREA = "AA"
+------+------+------+
| 2 | | AA |
+------+------+------+
I've tried everything I can think of. Different joins and sub-queries, but I can't seem to make this work.
You can try to write the condition in the subquery, then do self-join by the subquery.
JOIN condition on AREA and GROUP columns, if GROUP is null addition to check id.
CREATE TABLE T(
ID INT,
`GROUP` VARCHAR(5),
AREA VARCHAR(5)
);
INSERT INTO T VALUES ( 1 , 'A' , 'AA' );
INSERT INTO T VALUES ( 2 , NULL , 'AA' );
INSERT INTO T VALUES ( 3 , 'A' , 'AA' );
INSERT INTO T VALUES ( 4 , 'B' , 'AA' );
INSERT INTO T VALUES ( 5 , NULL , 'BB' );
INSERT INTO T VALUES ( 6 , 'A' , 'AA' );
INSERT INTO T VALUES ( 7 , 'B' , 'BB' );
INSERT INTO T VALUES ( 8 , NULL , 'AA' );
Query 1:
SELECT t1.*
FROM T t1 INNER JOIN (
SELECT *
FROM T
WHERE ID = 2 AND AREA = "AA"
) t2 ON t1.AREA = t2.AREA and t1.GROUP = t2.GROUP or (t2.GROUP is null and t1.id = t2.id)
Results:
| ID | GROUP | AREA |
|----|--------|------|
| 2 | (null) | AA |
Query 2:
SELECT t1.*
FROM T t1 INNER JOIN (
SELECT *
FROM T
WHERE ID = 1 AND AREA = "AA"
) t2 ON t1.AREA = t2.AREA and t1.GROUP = t2.GROUP or (t2.GROUP is null and t1.id = t2.id)
Results:
| ID | GROUP | AREA |
|----|-------|------|
| 1 | A | AA |
| 3 | A | AA |
| 6 | A | AA |
I am not quite sure I understand you fully, but if you want to get ID by GROUP, perhaps you can try
select distinct ID
from TABLE
group by GROUP
Try this:
SELECT * FROM `table_name` WHERE `ID` = 2 AND `AREA` = 'AA' AND `GROUP` IN (SELECT `GROUP` FROM `table_name` WHERE `ID` = 2);
PS: don't forget to change table_name by the table you're fetching records from.
I'd do this with a UNION
SELECT
ID
,`GROUP`
,AREA
FROM
your_table
WHERE ID = 2
UNION
SELECT
ID
,`GROUP`
,AREA
FROM
your_table
WHERE
`GROUP` = (SELECT `GROUP` FROM your_table WHERE ID = 2)
AND NULLIF(`GROUP`, '') IS NOT NULL;

How to get column which is assigned under another column

I have 2 tables.
This is my GroupTable.
CREATE TABLE GroupTable(
ID INT,
GROUPNAME VARCHAR(50),
UnderGroupId INT
);
INSERT INTO GroupTable VALUES (1,'A',0);
INSERT INTO GroupTable VALUES (2,'B',1);
INSERT INTO GroupTable VALUES (3,'C',2);
INSERT INTO GroupTable VALUES (4,'D',3);
Below is the datatable where i'm passing groupId in data table for reference
CREATE TABLE Reference(
ID INT,
GROUPID VARCHAR(50),
GroupValue VARCHAR(50)
);
INSERT INTO Reference VALUES (1,3,'X');
INSERT INTO Reference VALUES (2,4,'Y');
INSERT INTO Reference VALUES (3,1,'Z');
and i want to show the result like this
| ID | GROUPID | GroupValue | GROUPNAME1 | GROUPNAME2 | GROUPNAME3 | GROUPNAME4 |
|----|---------|------------|------------|------------|------------|------------|
| 1 | 3 | X | A | B | C | |
| 2 | 4 | Y | A | B | C | D |
| 3 | 1 | Z | A | | | |
From your comment, you can try to OUTER JOIN by GROUPID > UnderGroupId condition because that condition is those two table relationship condition.
then
UnderGroupId = 0 mean the group1
UnderGroupId = 1 mean the group2
UnderGroupId = 2 mean the group3
UnderGroupId = 3 mean the group4
You can do condition aggregate function on UnderGroupId, to get the pivot result.
TestDLL
CREATE TABLE GroupTable(
ID INT,
GROUPNAME VARCHAR(50),
UnderGroupId INT
);
INSERT INTO GroupTable VALUES (1,'A',0);
INSERT INTO GroupTable VALUES (2,'B',1);
INSERT INTO GroupTable VALUES (3,'C',2);
INSERT INTO GroupTable VALUES (4,'D',3);
CREATE TABLE Reference(
ID INT,
GROUPID VARCHAR(50),
GroupValue VARCHAR(50)
);
INSERT INTO Reference VALUES (1,3,'X');
INSERT INTO Reference VALUES (2,4,'Y');
INSERT INTO Reference VALUES (3,1,'Z');
Query 1:
SELECT t1.ID,
t1.GROUPID,
t1.GroupValue,
coalesce(MAX(CASE WHEN UnderGroupId = 0 THEN tt.GROUPNAME end),'') GROUPNAME1,
coalesce(MAX(CASE WHEN UnderGroupId = 1 THEN tt.GROUPNAME end),'') GROUPNAME2,
coalesce(MAX(CASE WHEN UnderGroupId = 2 THEN tt.GROUPNAME end),'') GROUPNAME3,
coalesce(MAX(CASE WHEN UnderGroupId = 3 THEN tt.GROUPNAME end),'') GROUPNAME4
FROM Reference t1
LEFT JOIN GroupTable tt ON t1.GROUPID > tt.UnderGroupId
GROUP BY t1.ID,
t1.GROUPID,
t1.GroupValue
Results:
| ID | GROUPID | GroupValue | GROUPNAME1 | GROUPNAME2 | GROUPNAME3 | GROUPNAME4 |
|----|---------|------------|------------|------------|------------|------------|
| 1 | 3 | X | A | B | C | |
| 2 | 4 | Y | A | B | C | D |
| 3 | 1 | Z | A | | | |

SQL: Join 2 tables and get only one result per condition from the first table

I have the following tables:
Auction-table
Id | Title | Description
------------------------
1 | Test1 | DescriptionForTest1
2 | Test2 | DescriptionForTest2
3 | Test3 | DescriptionForTest3
and
Bids-Table
AuctionId | UserId | Bidding
----------------------------
1 | 2 | 10
1 | 32 | 24
1 | 2 | 30
1 | 46 | 50
2 | 13 | 5
2 | 20 | 10
and so on...
Now I want a output, similar to this
AuctionId | Title | Description | UserId
----------------------------------------
1 | Test1 | Desc... | 46
2 | Test2 | Desc... | 20
I need one row per auction with the UserId with the highest Bidding.
I know that I could do this with 2 SQL-Statements. First getting all auctions and second with MAX(Bidding). But I need this in one statement and I'm stucking to get a working SQL-Statement.
Could anyone help me please?
I'm using MySQL as DBMS.
DROP TABLE IF EXISTS auction;
CREATE TABLE auction
(auction_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,Title VARCHAR(12) NOT NULL UNIQUE
,Description VARCHAR(30) NOT NULL
);
INSERT INTO auction VALUES
(1 ,'Test1','DescriptionForTest1'),
(2 ,'Test2','DescriptionForTest2'),
(3 ,'Test3','DescriptionForTest3');
DROP TABLE IF EXISTS bids;
CREATE TABLE bids
(auction_id INT NOT NULL
,user_id INT NOT NULL
,bid INT NOT NULL
,PRIMARY KEY (auction_id,user_id,bid)
);
INSERT INTO bids VALUES
(1 ,2 ,10),
(1 ,32 ,24),
(1 ,2 ,30),
(1 ,46 ,50),
(2 ,13 ,5),
(2 ,20 ,10);
SELECT a.*
, b.user_id
, b.bid
FROM auction a
JOIN bids b
ON b.auction_id = a.auction_id
JOIN
( SELECT auction_id, MAX(bid) max_bid FROM bids GROUP BY auction_id) c
ON c.auction_id = b.auction_id
AND c.max_bid = b.bid;
+------------+-------+---------------------+---------+-----+
| auction_id | Title | Description | user_id | bid |
+------------+-------+---------------------+---------+-----+
| 1 | Test1 | DescriptionForTest1 | 46 | 50 |
| 2 | Test2 | DescriptionForTest2 | 20 | 10 |
+------------+-------+---------------------+---------+-----+
Strawberry's answer is one way. Here is a second way. Try them both and see which one is faster on your setup. Indexes and table sizes will make a big difference.
SELECT High_Bid.AuctionId, A.Title, A.Description, High_Bid.UserID FROM
(
SELECT * FROM
(
SELECT * FROM bids_table
ORDER BY AuctionId, Bidding DESC
) AS B1
GROUP BY AuctionId
) AS High_Bid
JOIN
auction_table AS A
ON High_Bid.AuctionId=A.Id
Query:
SQLFIDDLEExample
SELECT t.*
FROM (
SELECT a.Id AS AuctionId,
a.Title,
a.Description,
(SELECT b.UserId
FROM Bids b
WHERE b.AuctionId = a.Id
ORDER BY b.Bidding DESC
LIMIT 1) as UserId
FROM Auction a) t
WHERE T.UserID is not null
Result:
| AUCTIONID | TITLE | DESCRIPTION | USERID |
----------------------------------------------------
| 1 | Test1 | DescriptionForTest1 | 46 |
| 2 | Test2 | DescriptionForTest2 | 20 |
It works with this sql:
select ID, Title, Description, UserId from Auction a
right join Bids b on a.ID = b.AuctionId
right join (select AuctionId, max(Bidding) as Bidding from Bids group by AuctionId) c on b.AuctionId = c.AuctionId and b.Bidding = c.Bidding;
Fiddle is here: fiddle
Result with a third user:
ID TITLE DESCRIPTION USERID
1 Test1 DescriptionForTest1 46
2 Test2 DescriptionForTest2 20
3 Test3 DescriptionForTest3 23
SELECT MAX(BINDING),A.AUCTION_ID,A.DESCRIPTION,A.TITLE,B.USER_ID
FROM AUCTION_TABLE A,BIDS_TABLE B
WHERE A.AUCTION_ID=B.AUCTION_ID
OR TRY WITH JOINS
SELECT MAX(BINDING),A.AUCTION_ID,A.DESCRIPTION,A.TITLE,B.USER_ID
FROM AUCTION_TABLE A
INNER JOIN BIDS_TABLE B ON A.AUCTION_ID=B.AUCTION_ID

SQL Query selecting row with unique record in a table's column?

I'm having a problem with my query by selecting the rows with unique id in one column.
Here's my db looks like:
Table 1
user_id | user_name | user_email
1 john ex0#email
2 nathel ex1#email
3 bob ex2#email
Table 2
subs_id | user_id | prod_id
1 1 1
2 1 2
3 2 1
4 3 1
5 3 3
Table 3
prod_id | prod_name
1 Platinum
2 Gold
3 Steel
What I need to do is to select the user row from Table 1 join with Table 2 & 3 that has a unique record in Table 2 user_id column based on their subscription.
Example. I want to select the user subscribing only in platinum, so the output must be looks like this :
user_id | user_name | user_email | subs_id | user_id | prod_id | prod_id | prod_name
2 nathel ex1#email 3 2 1 1 Platinum
SELECT * from users u inner join subscription s on u.user_id = s.user_id
inner join products p on p.prod_id = s.prod_id
WHERE s.user_id IN (SELECT user_id from subscription GROUP by user_id HAVING count(user_id) = 1) AND s.prod_id = 1
The last piece of the puzzle has been omitted as an exercise for the reader...
DROP TABLE IF EXISTS users;
CREATE TABLE users
(user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,user_name VARCHAR(20) NOT NULL UNIQUE
,user_email VARCHAR(12) NOT NULL
);
INSERT INTO users VALUES
(1 ,'john','ex0#email'),
(2 ,'nathel','ex1#email'),
(3 ,'bob','ex2#email');
DROP TABLE IF EXISTS product_user;
CREATE TABLE product_user
(product_id INT NOT NULL
,user_id INT NOT NULL
,PRIMARY KEY (product_id,user_id)
);
INSERT INTO product_user VALUES
(1, 1),(2 ,1),(1 ,2),(1,3),(3,3);
DROP TABLE IF EXISTS products;
CREATE TABLE products
(product_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,product_name VARCHAR(12) NOT NULL UNIQUE);
INSERT INTO products VALUES (1 ,'Platinum'),(2,'Gold'),(3,'Steel');
SELECT *
FROM product_user x
LEFT
JOIN product_user y
ON y.user_id = x.user_id
AND y.product_id <> x.product_id
WHERE x.product_id =1;
+------------+---------+------------+---------+
| product_id | user_id | product_id | user_id |
+------------+---------+------------+---------+
| 1 | 1 | 2 | 1 |
| 1 | 2 | NULL | NULL |
| 1 | 3 | 3 | 3 |
+------------+---------+------------+---------+
SELECT *
FROM product_user x
LEFT
JOIN product_user y
ON y.user_id = x.user_id
AND y.product_id <> x.product_id
JOIN users u
ON u.user_id = x.user_id
JOIN products p
ON p.product_id = x.product_id
WHERE p.product_name = 'Platinum';
+------------+---------+------------+---------+---------+-----------+------------+------------+--------------+
| product_id | user_id | product_id | user_id | user_id | user_name | user_email | product_id | product_name |
+------------+---------+------------+---------+---------+-----------+------------+------------+--------------+
| 1 | 1 | 2 | 1 | 1 | john | ex0#email | 1 | Platinum |
| 1 | 2 | NULL | NULL | 2 | nathel | ex1#email | 1 | Platinum |
| 1 | 3 | 3 | 3 | 3 | bob | ex2#email | 1 | Platinum |
+------------+---------+------------+---------+---------+-----------+------------+------------+--------------+
Hope so you are looking for this :
SELECT * FROM table1 as t1
inner join table2 as t2 on t1.user_id = t2.user_id
inner join table3 as t3 on t2.prod_id = t3.prod_id
where t3.prod_name = 'Platinum';
I think you can use DISTINCT to fetch unique recodes from database in query
SELECT DISTINCT
t1.`user_id` ,t1.`user_name`,t1.`user_email`,t2.`subs_id`,t2.`user_id`,t2.`prod_id`,t3.`prod_id` ,t3.`prod_name`
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.`user_id`=t2.`user_id`
INNER JOIN Table3 t3
ON t2.`prod_id`=t3.`prod_id`
WHERE
t1.`user_id`='2'
Hope this will be helpful to you
Please! try below code snip
select t1.user_id ,t1.user_name,t1.user_email,
t2.subs_id,t2.user_id , t2.prod_id ,
t3.prod_id , t3.prod_name
from table2 t2 inner join table1 t1 on t2.user_id=t1.user_id
inner join table3 t3 on t3.prod_id=t2.prod_id and t2.prod_id =1