JOIN in two tables in MySQL - mysql

I have a two tables in MySQL. I want to create a SELECT that will work in the following way:
Select from the table s_articles_supplier those lines whose id is equal to the active = 1
s_articles_supplier:
id | name
100 | Nike
101 | Adidas
s_articles:
supplierID | active
100 | 1
101 | 0

Use simple join with where condition
select a.id, name from s_articles_supplier a
inner join s_articles b on a.id=b.id
where active=1

You have to use inner join in following way for expected result
SELECT id, name FROM s_articles_supplier
INNER JOIN s_articles ON s_articles_supplier.id=s_articles.supplierID
WHERE s_articles.active=1
Hope it helps you

Related

mySQL Subselect with multiple results should be shown in 1 field

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

how to select one table then join another table mysql

so i have two tables.
users
users_id | firstname | lastname
10001 | mike | lapiz
10002 | tom | jerry
profile
profile_id | department | specialization
10001 | Health Dept | Heart
10002 | Brain Dept | Brain
maybe you're curious why i separate the name of the user and the profile.. i have my on reasons for that.. what i wanted to do is to select my all fields from profile then join the users table
what i want to be the result is
users_id | firstname | lastname | profile_id | department |specialization
10001 | mike | lapiz | 1001 | health dept | heart
this is my query..
$sql = SELECT a.profile_id,a.department,a.specialization FROM `tbl_profile` AS a LEFT JOIN (SELECT users_id,firstname,lastname FROM `tbl_users`) AS b ON a.profile_id = b.users_id
what happen is it only display the profile table.. it is not displaying the other table.. and when i tried to
LEFT JOIN (SELECT b.users_id,b.firstname,b.lastname FROM `tbl_users`) AS b
it give me an error unknown column b.users_id
You misunderstand how a join works.
FROM tbl_profile JOIN tbl_users ON ...
joins the two tables, i.e. combines records on the given condition in ON.
FROM tbl_profile JOIN (SELECT * FROM tbl_users)
does exactly the same. It makes no difference if you join a table directly or join the records of the table, because this means exactly the same.
FROM tbl_profile JOIN (SELECT users_id, firstname, lastname FROM tbl_users)
again does the very same thing. Only that you restrict the columns you can use in the query to the three stated columns. So if there existed more columns in the table, you could not use them in the query's select or where or order by clause anymore.
So a join means just combining records. Which columns you want to show, you put in the select clause:
SELECT * FROM tbl_profile JOIN tbl_users ON ...
selects all columns from both tables.
SELECT p.department FROM tbl_profile p JOIN tbl_users u ON ...
selects only the department.
You want:
SELECT * FROM tbl_users u JOIN tbl_profile p ON p.profile_id = u.user_id
A LEFT JOIN by the way is an outer join where you keep the records from the left table in your results even when there is no match in the right table. In your query you said that you wanted to show profile records too that have no match in the users table, which was certainly not intended.
You should use inner join not nested inner join
$sql = SELECT a.profile_id,a.department,a.specialization,b.users_id,b.firstname,
b.lastname FROM tbl_profile AS a inner join tbl_users b
ON a.profile_id = b.users_id
As you want to list all the columns in both the tables with LEFT OUTER JOIN, the following query will serve your purpose:
SELECT * FROM users LEFT OUTER JOIN profile on users.users_id = profile.profile_id
You can use the alias as well if you want as following:
SELECT * FROM users u LEFT OUTER JOIN profile p on u.users_id = p.profile_id

How to show value that has column with multiple value in another table in MySQL

I have A table:
Then B table:
Last one, C table:
I need to show them to be like this:
performance_id | quiz_id
________________________
22 | 65
23 | null
24 | 43
25 | null
I tried join but it show wrong result. It not show quiz id. I tried this:
SELECT A.performance_id, C.quiz_id
FROM A
LEFT JOIN B ON A.performance_id=B.performance_id
LEFT JOIN C ON B.phc_id = C.phc_id
group BY A.performance_id;
result:
Help me,thanks
Use group_concat:
SELECT A.performance_id, group_concat(C.quiz_id)
FROM A
LEFT JOIN B ON A.performance_id=B.performance_id
LEFT JOIN C ON B.phc_id = C.phc_id
group BY A.performance_id;
Because you get more than one quiz_id
Try this
SELECT A.performance_id, group_concat(C.quiz_id)
FROM A
LEFT JOIN B ON A.performance_id = B.performance_id
LEFT JOIN C ON B.phc_id = C.phc_id
group BY A.performance_id;
SQL Fiddle

Select join multiple tables in mysql?

I have three tables I'm trying to select data from, each table has a pID which is what I want the join to be based on. When I run the following query I still end up with three pID fields.
What is wrong with my select join statement?
SELECT * FROM Player p
LEFT JOIN AvgStats a ON a.pID = p.pID
LEFT JOIN MisTotal m ON m.pID = p.pID;
Player Table
pID | Name | Age
AvgStats Table
pID | 3pt% | gamePoints
MisTotal Table
pID | Fouls | rebounds
I want to creat a table that returns
pID | Name | Age | 3pt% | gamePoints | Fouls | rebounds
If I'm understanding your question correctly, just remove * from your query and specify the field(s) you want -- in this case, p.pID:
SELECT p.pId FROM Player p
JOIN AvgStats a ON a.pID = p.pID
JOIN MisTotal m ON m.pID = p.pID;
Given your edits, this should work:
SELECT p.pID, p.Name, p.Age, a.`3pt%`, a.gamePoints, m.fouls, m.rebounds
...
Just make sure you include the backticks around the column with the special character.

mysql - selecting groups and users all in same query

I have following two tables 'USERS' and 'GROUPS':
USERS
-id
-name
-groupid
GROUP
-id
-name
I'd like to return all users along with their group's name and group id. It should be an outer join on group id field correct?
A simple INNER JOIN should be enough:
SELECT `USERS`.*, `GROUP`.name AS group_name
FROM `USERS`, `GROUP`
WHERE `USERS`.groupid = `GROUP`.id
You're going to want to look at the JOIN statement
Doing this from my phone, so pardon any moderately incorrect syntax, but something a long the lines of
Edit: other guy's syntax is better. It's too early here
You can use a LEFT JOIN between users and groups so that users who are not in a group still show up in the result set, but with group name and id NULL:
SELECT
a.*,
b.name AS group_name
FROM
users a
LEFT JOIN
`group` b ON a.group_id = b.id
Side note: Ensure that you're encasing the table name group in backticks because it is a reserved keyword.
The result-set should look something like:
id | name | group_id | group_name
-----------------------------------------------------------------------------
1 | John | 5 | ThisIsGroup5
3 | Tim | 3 | ThisIsGroup3
6 | NotInGroup | NULL | NULL
Changing LEFT to INNER in the above query would INNER JOIN the two tables and exclude the user "NotInGroup" from the result-set.