showing output from two tables but matching them via 3rd table - mysql

Suppose i have three table. first table got wine_name and wine_id, 2nd table got wine_id,varie_id and the 3rd table got variety_id and variety_name. I just want to show the wine name with the variety name. I tried inner join but cdnt get the result, jst getting error. can some suggest me what should i write.

Are you looking for this?
SELECT w.wine_name, v.variety_name
FROM wine_variety wv JOIN wines w
ON wv.wine_id = w.wine_id JOIN varieties v
ON wv.variety_id = v.variety_id
Sample output:
| WINE_NAME | VARIETY_NAME |
----------------------------
| Wine 1 | Variety 1 |
| Wine 2 | Variety 2 |
Here is SQLFiddle demo

Try below code
select table1.wine_name, table3.variety_name from table1 inner join
table2 on table1.wine_id = table2.wineid left
join table3 on table2.varieId = table3.varietyID

Related

SQL Nested Query to select in two tables

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.

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

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.

Joining 2 tables with same id PDO

I am sorry this is not a high quality question and I know I am risking downvotes, but I am trying to learn as I go. I am currently working on a side project and stumbled into a situation I am not sure of.
I have two tables and need to call the data from both sharing the same id number (different names)
I will now attempt to give an example
Table 1
| psid | idd |
| 1 | 999 |
| 2 | 42 |
Table 2
| aid | other |
| 999 | hello world |
| 42 | welcome |
I am trying to link idd and aid whilst displaying all rows from table one
Example
id = 1 / Title : hello world
id = 2 / Title : welcome
I am not sure if this can be achieved with a single query to the database I have tried adding a second but it goes in a nonstop loop.
I have not done much searching as not sure what to search for.
Thanks and sorry
Cartesian Join
SQLFiddle
select
table1.*,
table2.*
from
table1,
table2
where
table1.idd = table2.aid and
table1.idd = :id
Or Left Join
SQL Fiddle
select
t1.*,
t2.*
from
table1 t1
left join
table2 t2
on
t1.idd = t2.aid
where
t1.idd = :id
SELECT table1.psid, table2.other FROM table1
JOIN table2 ON table1.idd = table2.aid
WHERE table1.idd= 'X' AND table2.aid = 'X'
this should JOIN the two tables together and by specifying the matching id's for each table in the WHERE clause should get the relevant information.
EDIT fixed SQL

MySQL join multiple rows from one table while selecting only a single row from the others

Maybe a bit of a strange title description, but i basically want to achieve something the GROUP_CONCAT() function does, only then keep the double entries.
I have four tables i want to join, client, doctor, physio and records
Depending on the variable $client i want to get the client details, attending doctor and therapist (one single row from three tables) and join all records for that user.
Say that in this case the $client = 1. The records table has five records where the column r_client_id = 1. If i run a query like below i only get one record from the records table, namely the first occurrence where r_client_id = 1 (which makes sense of course):
SELECT
client.c_id, client.c_name
doctor.d_name,
physio.p_name,
records.r_record
FROM
adm_clients AS client
INNER JOIN
norm_client_doctor AS ncd ON ncd.ncd_client_id = client.c_id
INNER JOIN
adm_doctor AS doctor ON doctor.d_id = ncd.ncd_doctor_id
INNER JOIN
norm_client_physio AS ncp ON ncp.ncp_client_id = client.c_id
INNER JOIN
adm_physio AS physio ON physio.p_id = ncp.ncp_physio_id
LEFT JOIN
adm_doctor_records AS records ON records.r_client_id = client.c_id
WHERE
client.c_id = '".$client."'
Now assume the five records where r_client_id = 1 are like so:
+------+-------------+-------------------+----------+
| r_id | r_client_id | r_record | r_date |
+------+-------------+-------------------+----------+
| 1 | 1 | regular visit | 10/10/12 |
+------+-------------+-------------------+----------+
| 3 | 1 | emergency control | 24/10/12 |
+------+-------------+-------------------+----------+
| 7 | 1 | regular visit | 08/09/12 |
+------+-------------+-------------------+----------+
| 18 | 1 | delivery | 03/01/12 |
+------+-------------+-------------------+----------+
| 20 | 1 | health checkup | 10/12/11 |
+------+-------------+-------------------+----------+
I want my output to be in an array like so:
Client 1
- Name Doctor
- Name Physio
Records
- Emergency control, 24/10/12
- Regular visit, 10/10/12
- Regular visit, 08/09/12
- Delivery, 03/01/12
- Health checkup, 10/12/11
The closest one i can image is a to add a GROUP_CONCAT() on the records, but that, of course, groups the 'regular visit', so i'll get 4 rows instead of 5
GROUP_CONCAT(DISTINCT records.r_record SEPARATOR '|')
[..]
echo(str_replace("|","<br>",$show->r_record));
Anybody an idea how to display all the matching records? I have the feeling i'm close, but i'm out of options by now..
Edit:
I forgot to mention that when i remove the DISTINCT, it displays all the records twice..
SOLVED:
Got it working like so:
GROUP_CONCAT(DISTINCT
CONCAT (records.r_date, '~', records.r_record, '~', records.r_paraph)
SEPARATOR '|') AS clientDoctorRecords,
Try:
SELECT
client.c_id, client.c_name
doctor.d_name,
physio.p_name,
GROUP_CONCAT(records.r_record)
FROM
adm_clients AS client
INNER JOIN
norm_client_doctor AS ncd ON ncd.ncd_client_id = client.c_id
INNER JOIN
adm_doctor AS doctor ON doctor.d_id = ncd.ncd_doctor_id
INNER JOIN
norm_client_physio AS ncp ON ncp.ncp_client_id = client.c_id
INNER JOIN
adm_physio AS physio ON physio.p_id = ncp.ncp_physio_id
LEFT JOIN
adm_doctor_records AS records ON records.r_client_id = client.c_id
WHERE
client.c_id = '".$client."'
GROUP BY
client.c_id
If you want r_date to come along with record in one column, then you can use plain CONCAT first and then do a GROUP_CONCAT on it.