I have this app where I need to do a query and have two columns.This are my two columns and respective rows:
Name of table1: Machines(has a row called Machinesnames and a id_group as FK)
Name of table2: Groups (has a row called groupsnames and id_groups as PK)
The problem is that with the query you see below I am getting the following result
**GroupsNames** | **MachinesNames**
1 machine1
1 | machine2
1 | machine3
2 | machine4
I have done this but I think is wrong can you correct my query please?:
SELECT groups.name,Machines. Machinesnames,Groups.groupsnames FROM Machines INNER JOIN Groups ON Machines.id_group = Groups.id_group
This is the result I want to see
**GroupsNames** | **MachinesNames**
1 machine1,machine2,machine3
2 | machine4
You are looking for group_concat:
select g.name,
group_concat(m.Machinesnames)
from Machines m
inner join Groups g on m.id_group = g.id_group
group by g.name;
Your query is correct for a inner join, but from looking at your expected output you are wanting a aggregated list.
Try this answer for MySQL using GROUP_CONCAT()
Aggregate function in MySQL - list (like LISTAGG in Oracle)
Related
How to you group concat 2 simple tables so the the output is:
John | One, Two
Luke | One, Two
I have seem some other examples, but I don't appear to make sense of it. Im a complete NOOB
users table
|name|names|
|John|001|
|John|002|
|Luke|001|
|Luke|002|
servers table
|id|name|
|001|One|
|002|Two|
MYSQL statement
SELECT *
FROM users
INNER JOIN servers
ON users.names = servers.names
Produces:
John | One
John | Two
Luke | One
Luke | Two
Use GROUP_CONCAT as follows:
SELECT u.name, GROUP_CONCAT(s.name ORDER BY s.id) names
FROM users u
LEFT JOIN servers s ON s.id = u.names
GROUP BY u.name;
Demo
My database has two tables.
food_table:
+----+----------+---------------+
| ID | NAME | NutrientID |
+----+----------+---------------+
nutrient_table:
+------------+--------------+
| NutrientID | NutrientName |
+------------+--------------+
I want select all rows in food table, but get NutrientName instead of nutrientID.
If I do:
select * from food_table.
I will get NutrientID. Is it possible to get NutrientName in a single query?
SELECT f.*, n.NutrientName FROM food_table f
LEFT JOIN nutrient_table n
ON n.NutrientID = f.NutrientID
You need to make a join inner to tables filtering by field NutrientID , if you look this field as the same in two table and the join works fine.
I want to have shown multiple results in 1 fields for a subselect.
As example:
table 1:
tbl1_ID, fistname, familyname
table 2:
tbl2_ID, carbrand
table 3 is the n:n relationship for table 1 and 2
tbl1, tbl2
The Person of table 1 should be able to own several cars (for example Ford and BMW).
The car brand of table 2 is applicable to several People of course.
I want to have listed the cars of each Person in 1 data field
Example:
Mueller | Hans | Ford,BMW
Jaeger | Erwin | BMW,Mercedes,Jaguar
Fritsche | Sascha | Mercedes
How to do this? I cannot do with subselect because it allows only 1 result.
Also, it doesn't work with LEFT JOIN because I want to have shown each Person once only.
Thanks! MR
You could use group_concat and you should use inner join between the two related tables based on table 3 and group by
select a.familyname, a.fistname, group_concat(b.carbrand)
from table_3 c
inner join table1 a on c.table1_id = a.table1_id
inner join table2 b on c.table2_id = b.table2_id
group by a.familyname, a.fistname
MySQL: How to count rows when multiple joined tables ??
+Query:
SELECT p.role FROM users u
INNER JOIN roles r ON r.id=u.role
INNER JOIN permissions p ON p.role=r.id
WHERE p.p_id=19 AND p.p_update=1 AND r.active=1
GROUP BY p.role
+Result:
________
|p_role:|
|_______|
| 1 |
| 4 |
|_______|
When I add COUNT() function to p.role:
+Query:
SELECT COUNT(p.role) FROM users u
INNER JOIN roles r ON r.id=u.role
INNER JOIN permissions p ON p.role=r.id
WHERE p.p_id=19 AND p.p_update=1 AND r.active=1
GROUP BY p.role
+Result:
_______________
|COUNT(p_role):|
|______________|
| 5 |
| 1 |
|______________|
Why I see 2 records I already use COUNT() function??
I don't want to see result like this.
I want to count rows of query above.
I want to see result like this:
_______________
|COUNT(p_role):|
|______________|
| 2 |
|______________|
Please anyone help me. Thanks in advance!
Sorry for my broken English.
Because of GROUP BY clause in your query, it returns count per p.role value. If you want the query to return one value, you can remove GROUP BY, e.g.:
SELECT COUNT(DISTINCT(p.role)) FROM users u
INNER JOIN roles r ON r.id=u.role
INNER JOIN permissions p ON p.role=r.id
WHERE p.p_id=19 AND p.p_update=1 AND r.active=1;
By the way, I have added DISTINCT to the query so that it will return count of distinct p.role values. If you want the count of records only, you can remove DISTINCT.
I have a table named phpbb_pcp_market with these rows: http://pastebin.com/ZAFjawD8 (There are more obviously)
And I have another table named phpbb_pcp_market_cart that looks like this:
+----+---------+-----------+------------+
| id | item_id | player_id | time |
+----+---------+-----------+------------+
| 14 | 49 | 3 | 1384806292 |
+----+---------+-----------+------------+
I need to join these two tables based on item_id, but for some reason it's not working.
This is the query I've used:
SELECT m.*, c.* FROM (phpbb_pcp_market_cart c)
LEFT JOIN phpbb_pcp_market m
ON (c.item_id = m.item_id)
WHERE c.player_id = 3
ORDER BY c.time
For some reason, it's returning nothing.
I can't figure what I did wrong in the query. And no, I'm not good at SQL.
Everything looks fine with your SQL-code.
Look in the rest of your PHP-code if there is something wrong. The bug is not related to the SQL-part ;)
First double check your data, your query seems to be OK.
If you want to select all items for specific player_id don't use LEFT JOIN but simple JOIN, because you will never get rows where it could be NULL.
Also braces can be left out for simplicity:
SELECT m.*, c.* FROM phpbb_pcp_market_cart c
JOIN phpbb_pcp_market m
ON c.item_id = m.item_id
WHERE c.player_id = 3
ORDER BY c.time