This question already has answers here:
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 1 year ago.
I have one table called forms and one table called categories. They have a many-to-many relationship through a category_form as pivot. The pivot table has form_id and category_id
So each form can have many categories and every category can be attached to many forms.
I need to aggregate this data somehow. What I need is a comma separated list of category id's in a column for each form.
Like this:
FORMS
id | name | categories
1 | f1 | 1,4,7
2 | f2 | 1
3 | f3 | 1,6,8,9
What would be an effective way of doing this?
Try this:
SELECT f.id, f.name, GROUP_CONCAT(cf.category_id) AS categories
FROM forms f
INNER JOIN category_form cf ON f.id = cf.form_id
GROUP BY f.id, f.name
ORDER BY f.id;
Related
This question already has answers here:
Why does using a subquery inside a left join give a completely different answer than an equivalent table?
(2 answers)
Access 2007 - Left Join to a query returns #Error instead of Null
(3 answers)
Closed 1 year ago.
I have two tables in Access:
Table1:
ID
1 A
2 B
3 C
Table2:
ID
1 A
2 B
3 D
and a query (Query1) that adds a new column to Table 1:
SELECT [Table1].ID
,"NEW CATEGORY" AS Category
FROM [Table1]
Which looks like this:
ID Category
1 A NEW CATEGORY
2 B NEW CATEGORY
3 C NEW CATEGORY
Now, if I join Query1 to Table2, like this:
SELECT [Table2].ID
,[Query1].ID
,[Query1].Category
FROM [Table2]
LEFT JOIN [Query1] ON ([Table2].ID = [Query1].ID)
I get the following result:
Table2.ID Query1.ID Category
1 A A NEW CATEGORY
2 B B NEW CATEGORY
3 D NEW CATEGORY
"NEW CATEGORY" magically appears in row #3 in the Category column, whereas I am expecting an empty cell there.
Any ideas of the source of this strange behavior, or how it can be avoided?
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
I have two tables in my database where the first one has related values of the second one. Just like this:
table "people"
ID | NAME | SCHOOL
-------------------------
1 | john | 2
2 | fred | 1
3 | maria | 3
table "school"
ID | NAME
-------------------------
1 | first school
2 | second school
3 | third school
Ok.
I'm trying to make a select in "people" table and get the "SCHOOL" number replaced by "school" table related id.
So I did this:
"SELECT * FROM people A LEFT JOIN school B ON A.school = B.id"
That's ok!
But If I have to get the "people" ID value in this return, it will be replaced by "school" table ID value.
How can I solve this?
Thanks a lot!
If you are learning SQL, learn to list all the columns you want. Explicitly:
SELECT p.id, p.name, s.name as school_name
FROM
people p LEFT JOIN
school s
ON p.school = s.id;
Notes:
The table aliases are abbreviations for the table names. This makes the query much easier to follow.
A LEFT JOIN is not really needed here, but you are using it.
s.name could be aliased (s.name as school_name) to distinguish it from the person's name.
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)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
problem in many to many relationship
I have a table of movies and a table of categories. I would like to register one more category for a film. And a category for several films. That is, a relation N: N. How do I do that in php and Mysql?
For example:
Categorie 1 -> Movie 1
and Movie 2
Movie 2 -> Categorie 1
and Categorie 2
For a many to many relationship, you need a third table called the junction table
So it would look like that
Movie
id | desc
Category
id | desc
MoviesCategories
id | movieID | categoryId
Your selects would be on MoviesCategories and would look like this
SELECT *
FROM
MoviesCategories INNER JOIN Category ON MoviesCategories.CategoryId = Category.Id
INNER JOIN movie ON MoviesCategories.MovieID = Movie.ID
You need a table for that relationship. Like this:
MovieToCategory
ID
CategoryID
MovieID
Alternatively you can create a composite primary index only allowing each movie to category combination one time:
MovieToCategory
CategoryID
MovieID