I have a query that looks like this:
SELECT T1.ID, T3.Val FROM Table1 as T1
INNER JOIN Table2 as T2 ON T1.ID = T2.ID
INNER JOIN Table3 as T3 ON T2.ID = T3.ID
My problem is that the last Inner Join returns 3 rows and i want to print all of those values like T3.val1, T3.val2, T3.val3
How is that done?
EDIT: Example data:
T3
DataID, DefinitionID, ItemID, UnitID, Val, TS
123 111 4541 45545 0.05 2016-05-07 06:14:07
124 111 5487 69587 0.026 2016-05-07 09:11:01
125 111 3621 12862 0 2016-05-07 10:04:17
This is now my query:
SELECT t1.EventID, group_concat(t3.Val) FROM T1 as t1
INNER JOIN T2 as t2 ON t1.EventID = t2.EventID
INNER JOIN T3 as t3 on t2.DefinitionID = t3.DefinitionID
GROUP BY t3.Val;
And the result is:
EventID group_concat(t3.val)
3 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0........
SELECT T1.ID, GROUP_CONCAT(T3.Val,',') FROM Table1 as T1
INNER JOIN Table2 as T2 ON T1.ID = T2.ID
INNER JOIN Table3 as T3 ON T2.ID = T3.ID
GROUP BY T3.ID
Table1
---------------------
ID
1
Table2
---------------------
ID
1
Table3
---------------------
ID Val
1 A
1 B
1 C
Output
ID Val
1 A,B,C
Related
i have 4 tables: table1, table2, table3, table4.
table1= user_id, username
table2= tbl2_user_id, tbl2_name
table3= tbl3_user_id, tbl3_name
table4= tbl4_user_id, tbl4_name
i want to select the users in table1 if they also are in one of: (table2 OR table3 OR table4).
example:
table1=user1, user2, user3, user4, user5, user6, user7, user8, user9, user10.
table2=user2, user5, user7.
table3=user3.
table4=user4, user9.
[result of table1 select: user2, user3, user4, user5, user7, user9].
I tried this code and did NOT work:
select t1.username
from table1 t1
INNER JOIN table2 t2
ON t1.user_id = t2.tbl2_user_id
INNER JOIN table3 t3
ON t1.user_id = t3.tbl3_user_id
INNER JOIN table4 t4
ON t1.user_id = t4.tbl4_user_id
Thank you,
INNER JOIN is not the correct way, that filters only when all 3 are met, you need a LEFT JOIN and a WHERE condition
A Left Join to not filter the query, and then a WHERE to see if any of them are valid
select t1.username
from table1 t1
LEFT JOIN table2 t2
ON t1.user_id = t2.tbl2_user_id
LEFT JOIN table3 t3
ON t1.user_id = t3.tbl3_user_id
LEFT JOIN table4 t4
ON t1.user_id = t4.tbl4_user_id
WHERE
(
t2.tbl2_user_id is NOT NULL OR
t3.tbl3_user_id is NOT NULL OR
t4.tbl4_user_id is NOT NULL
)
Here are 2 tables.
Table 1
id value
1 3
2 2
3 3
4 1
5 4
6 3
Table 2
id
1
3
4
How do I get the ids that are in Table 2 which have the max value in Table 1?
Output:
id
1
3
I already tried the following to get the max value, but I cannot figure out how to use it in a single query to get the matching rows. Because I think I need to select from the same table I just inner joined.
select max(table1.value)
from table2
inner join table1 on table1.id = table2.id;
Here is one method:
select t2.id
from (select t2.*, rank() over (order by value desc) as seqnum
from table2 t2 join
table1 t1
on t2.id = t1.id
) t
where seqnum = 1;
Or, an alternative that puts all the ids on one row:
select group_concat(t2.id) as ids
from table2 t2 join
table1 t1
on t2.id = t1.id
group by t1.value
order by t1.value desc
limit 1;
You have a couple of options available without using window functions:
You can use a WHERE clause to select only id values that have a value equal to the MAX(value) from your query and an id that is in Table2:
SELECT t1.id
FROM Table1 t1
WHERE value = (
SELECT MAX(t1.value)
FROM Table2 t2
JOIN Table1 t1 ON t1.id = t2.id
)
AND id IN (SELECT id FROM Table2)
You can JOIN your query to Table1 and Table2 again, matching the value in Table1 and the id in Table2:
SELECT t1.id
FROM (
SELECT MAX(t1.value) AS max_value
FROM Table2 t2
JOIN Table1 t1 ON t1.id = t2.id
) t
JOIN Table1 t1 ON t1.value = t.max_value
JOIN Table2 t2 ON t2.id = t1.id
In both cases the output is
id
1
3
Demo on SQLFiddle
Too low to comment but from the SQL statement you gave, you just need to add the tableid in your select parameters.
select table2.id, max(table1.value)
from table2
inner join table1 on table1.id = table2.id;
I have 2 tables in MySQL
table1
ID|name|group_1|group_2|group_3
Table2
ID|group_name|Group_location
I want to get table 1 ID and show all the data and not tableIDs.
I'm trying to use a join like so
SELECT table1.name, table2.group_1, table1.group_2, table1.group_3
FROM tabl1 JOIN table2 on table1.group_1 = table2.ID AND
table1.group_2 = table2.ID AND table1.group_3 = table2.ID
WHERE table1.ID IN (868)
ORDER BY FIELD(table1.ID,868);
It's only returning the IDs in the result but I want it to return the table2 group_name
Is this what you want?
SELECT t1.name, t21.group_1, t22.group_2, t23.group_3
FROM table1 t1 LEFT JOIN
table2 t21
ON t1.group_1 = t21.ID LEFT JOIN
table2 t21
ON t1.group_2 = t22.ID LEFT JOIN
table2 t21
ON t1.group_3 = t23.ID
WHERE t1.ID IN (868)
ORDER BY FIELD(t1.ID, 868);
This uses LEFT JOIN in case any of the reference columns are NULL.
Try this;
SELECT t1.name
, t21.group_name
, t22.group_name
, t23.group_name
FROM table1 t1
LEFT
JOIN table2 t21
ON t1.group_1 = t21.ID
LEFT
JOIN table2 t21
ON t1.group_2 = t22.ID
LEFT
JOIN table2 t21
ON t1.group_3 = t23.ID
WHERE t1.ID IN (868)
ORDER
BY FIELD(t1.ID, 868);
I have so far written the following mysql query which is joining 4 tables as follows:
SELECT
T3.fault, t4.name
FROM table2 T2
INNER JOIN (
SELECT a.*
FROM table1 a
LEFT JOIN table1 b ON a.item_id = b.item_id AND a.submit_id < b.submit_id
WHERE b.submit_id IS NULL
) T1 ON T1.item_id = T2.item_id
INNER JOIN table3 T3 ON T1.id = T3.run_id
LEFT JOIN table4 T4
ON 3.runname_id = T4.id
order by count(*) desc;
Below is sample Data for table 2 which has item_id as PK
Example query: select * from table2 where item_id = '15907';
item_id host
15907 abc.com
7303 cde.com
7304 abcd.com
7305 cdedf.com
I have now recently added a table table5 which looks as below and has item_id as PK. I want to join table5 with table2 on item_id and want to retrieve value for restoreid also in my final query.
item_id restoreId
15907 12342
7303 12342
7304 14342
7305 14342
How to implement join between table5 and table2 on item_id? My select query should also retrieve T5.restoreId along with T3.fault, t4.name
select your_table.fault,your_table.name,t5.restoreid
from (
SELECT
T3.fault, t4.name,t2.item_id
FROM table2 T2
INNER JOIN (
SELECT a.*
FROM table1 a
LEFT JOIN table1 b ON a.item_id = b.item_id AND a.submit_id < b.submit_id
WHERE b.submit_id IS NULL
) T1 ON T1.item_id = T2.item_id
INNER JOIN table3 T3 ON T1.id = T3.run_id
LEFT JOIN table4 T4
ON 3.runname_id = T4.id
) as your_table left join table5 t5 on your_table.item_id = t5.item_id
SELECT
T3.fault, t4.name, T5.restoreid
FROM table2 T2
INNER JOIN (
SELECT a.*
FROM table1 a
LEFT JOIN table1 b ON a.item_id = b.item_id AND a.submit_id < b.submit_id
WHERE b.submit_id IS NULL
) T1 ON T1.item_id = T2.item_id
INNER JOIN table3 T3 ON T1.id = T3.run_id
LEFT JOIN table4 T4
ON T3.runname_id = T4.id
LEFT JOIN table5 ON T2.item_id = T5.item_id;
Looks like I am not able to understand some of the solutions given here and in other places, so I decided to ask my own question.
I have three tables. Like this:
table1
id name active
123 item1 1
234 item2 0
345 item3 1 <--- not in table2!!
456 item4 1
567 item5 1
table2
id item_id instock variants deliverytimes
1 123 0 S 21days
2 123 1 M 21days
3 123 2 L 21days
4 456 1 white 10days
5 456 0 black 10days
6 234 0 yellow sold
7 456 1 green sold
8 456 0 red sold
9 567 0 big sold
table3
id item_id description
1 123 Cool Shirt
2 234 Collectors Box
3 345 Comicbook
4 456 Basecap OneSize
5 567 Cool-Mug
I tried several attempts from LEFT JOIN to RIGHT JOIN etc. that all ended up with multiple results that are not DISTINCT in the ID of the first table that I need nor they were complete. Means that if table2 does not comntain item of table1 it won't show in the result.
The closest I got is this:
select * from table1 t1
INNER JOIN (
SELECT * FROM table2
where (deliverytimes!='sold' OR instock>0) LIMIT 1 ) t2
ON t1.id=t2.item_id,
table3 t3
where t1.active = '1'
and t1.id = t2.item_id and t3.item_id = t1.id;
In the end I need a list of:
"id, name, description"
result (as I would like it to be)
id name description
123 item1 Cool Shirt
345 item3 Comicbook
456 item4 Basecap OneSize
that does need to meet this requirements:
"item needs to be t1.active=1"
and
"if items has rows in t2 show only if one row equals (instock>0 or deliverytimes!=sold)"
and
"if item has no rows in t2 show as long as t1.active=1"
Last one is the problem. I never get distinct t1.id when I use other than inner join and with inner join I still miss the rows that are not present in t2 but are still active=1.
When starting to write a query think about what data you want to show. In your case you want to show data from table1 and table3, so join these and select from them. The criteria on table2 belong in the WHERE clause.
The criteria on table2 are:
either no entry in t2 exists
or an entry in t2 exists with instock > 0 or deliverytimes != sold
This means one EXISTS clause, one NOT EXISTS clause, both combined with OR.
select t1.id, t1.name, t3.description
from t1
join t3 on t3.item_id = t1.item_id
where t1.active = 1
and
(
not exists
(
select *
from t2
where t2.item_id = t1.item_id
)
or
exists
(
select *
from t2
where t2.item_id = t1.item_id
and (instock > 0 or deliverytimes != 'sold')
)
);
try this
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
if you want to filter you result with active = 1 and != soled filter in where clause
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
WHERE t1.active = 1 AND t2.deliverytimes != 'sold'
OR
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id AND t2.deliverytimes != 'sold'
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
WHERE t1.active = 1
You can try to link table1 with table2 using Left outer join (this will make sure you will get all records from table1). And now link tbale3 as inner join, this will make sure to get all records which are related with table1.
Table1 LEFT OUTER JOIN TABLE2 and Table1 INNER JOIN table3.
Above is my assumption, you can try the above logic.