mySQL Subselect with multiple results should be shown in 1 field - mysql

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

Related

SQL - SELECTING from one table while INNER JOINING between two others (many-to-many table and another table)

I am a beginner to Web programming and SQL. I am not used to work with tables that have a many to many relationship and I am facing a problem here due to my lack of knowledge.
Those are my tables and this is what I want to do:
Table users
ID | Users
-----------------
1 | John
2 | Mark
3 | Sophia
Table projects
ID | Projects
-----------------
1 | Generic Name nº 1
2 | Generic Name nº 2
3 | Generic Name nº 3
Table users_projects
UsersID | ProjectsID
-----------------
1 | 1
2 | 1
2 | 2
3 | 2
3 | 3
I want to select all the users where, let's say, the Projects.Id value is 1, while keeping the many to many realtionship between this two tables. How do I do that?
Desired Output
ID | Users
-----------------
1 | John
2 | Mark
SELECT t1.*
FROM users t1
JOIN users_projects t2 ON t1.id = t2.usersid
or
SELECT *
FROM users t1
WHERE EXISTS ( SELECT NULL
FROM users_projects t2
WHERE t1.id = t2.usersid )
I recommend you to rename ID columns in users and projects and assign them the same names which are used in users_projects. This will remove ambiguity and will make your structure and queries more clear.
If you want the result for Projects.Id value is 1 try:
select u.ID,u.Users
from users u
inner join users_projects p on u.ID=p.UsersID
where p.ProjectsID=1;
CREATE TABLE users (
ID int,
Users varchar(10) );
INSERT INTO users VALUES
(1,'John'),
(2,'Mark'),
(3,'Sophia');
CREATE TABLE users_projects (
UsersID int,
ProjectsID int );
INSERT INTO users_projects VALUES
(1,1),
(2,1),
(2,2),
(3,2),
(3,3);
Result:
ID Users
1 John
2 Mark
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=463a1cfac69f07d41a2caa440decdb25
You can try below, by using left join you specify on which fields to join.
You can use table aliases and prefix the fields with the alias to indicate between which table the fields are linked.
select u.Users, u.ID, p.Projects, p.ID
from Users u
left join user_project up
on u.ID = up.UsersID
left join projects p
on up.ProjectsID = p.projects
order by u.Users, p.Projects

Using left join and keep value of left table ID

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.

MySQL Left Join with no matches on Table B, has no ID on Table A

I try doing somethin like this:
Table A
NameID | name | other rows
1 | name1
2 | nameA
3 | nameX
Table B
UniqueID | NameID | other rows
1 | 1
2 | 2
3 | 6
4 | 17
5 | 22
No I want to do a select like this:
SELECT tablea.ID, tablea.name, tableb.*
FROM tablea
LEFT JOIN tableb ON tablea.ID = tableb.ID
WHERE
tablea.ID != '0'
I search now for the ID and the name.
I get allways the name, but not allways the ID, only if the ID is in table B also. why?
My result is like this:
ID | name
1 | name1
2 | nameA
NULL | nameX
I have this situation:
I have in TABLE A films (with names, datas, etc.). In table B I have
ratings of these movies. Now I search with a form and want to see all movies, that have an incomplete rating, or no ratings at all, or any film despite of it has or not ratings.
With a LEFT JOIN on TABLE A (all movies) and with the addition ON-Condition I search if the movie has a rating or not (is or is not in TABLE B).
My result get me the name of the movie, allways. But if the movieID is not in table B (= no ratings recorded), I do not get the ID of the Movie in table A in the result.
So not my entire result is NULL only the movieID, I can pull any other DATA out of TABLE A for this movie, but not the ID.
so why?
Is it not possible to ask for the tablea.ID, if the ID is in the ON - term?
Left join will show all rows form the first table and add data from the second table when a matching rows is found.
If no matching row is found, left join will return null (which means "unknown") for those rows.
If you want to discard rows from you result that have no matching row in the other table, you have to use "inner join" instead of "left join".
Check visual representation of joins for more info: https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
The solution is, to set an alias for the row in table one, which is duplicated in the second table and has due to no "rows" in second table the outcome NULL.
If I put an alias into it, I can select it and LEFT JOIN will do the rest.
What to know: mysql_fetch_object will always select the last mention of a row, if the row name is identic in other tables of the select. In my case I LEFT JOIN a second table with the same row-name. Due to LEFT JOIN I have no results on the ON condition and the correct outcome for my query is NULL.

Mysql select between two table without limiting if record appear on the joined table

I have been trying to figure out how to select data related to one id between to tables without limit it to the joined table. I tried using UNION, Inner join, JOIN, but it limit me to show records that are only in both tables. By example:
Table 1 (users)
id | name | register
1 | John | 2014-03-01
2 | Kate | 2014-03-02
etc..
Table 2 (birthdays by example)
id | user | birthday
1 | 1 | 1989-09-09
Note that kate dont have a record on the birthdays table, if i do:
SELECT U.id, name, register, B.birthday FROM users as U INNER JOIN birthday as B ON B.user = U.id
it will only shows JOHN data, i would like to select all my users and if the record do not exist on the joined table, still be able to select all my users, sort of:
id | name | register | birthday
1 | John | 2014-03-01 | 1989-09-09
2 | kate | 2014-03-02 | null or ''
3
4
etc.
Sorry if its a stupid question but i dont find the light on this one. I would appreciate the help.
Regards
You need a LEFT OUTER JOIN instead of the plain JOIN (also known as INNER JOIN), like this:
SELECT U.id, name, register, B.birthday
FROM users as U
LEFT JOIN birthday as B
ON B.user = U.id
A LEFT JOIN between users and birthday tables will contain all records of the "left" table (users), even if the join-condition does not find any matching record in the "right" table (birthday).
This excellent article on The Code Project will help you a lot: Visual Representation of SQL Joins.
Summary of all JOIN types:
Note: Mysql does not support FULL OUTER JOIN but it can be emulated. Useful articles:
https://stackoverflow.com/a/4796911
http://www.sql-tutorial.ru/en/book_full_join_and_mysql.html
http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/
Use left outer join instead of inner join..
SELECT U.id, name, register, B.birthday
FROM users as U left join birthday as B ON B.user = U.id

Selecting data from two tables using Mysql stored procedures

I have to select data from two tables with following criteria,
lets say there are two tables as,,
Table one
id | itemName | Quantity | companyName
1 bread 25 the Baker pvt ltd
2 butter 30 green famers
Table two
id | itemName | itemPrice
1 bread 30.50
6 jam 80.25
what I need is,
select items out of two tables which their ids are matching and the quantities of them should be multiplied by the unit price if ids are matching. The rows which don't have matching ids should be selected but their quantities should not multiplied.
SELECT o.id, o.itemName, o.companyName, o.Quantity * IFNULL(t.itemPrice, 1) total
FROM one o
LEFT JOIN two t
ON o.id = t.id
Something like this should work ...
Select a.id, a.itemName, a.companyName, a.Quantity * IFNULL(b.itemPrice,1) As total
From table1 as a
Left Join table2 as b on a.id = b.id