MySQL Select Join on Multiple Tables - mysql

I have multiple tables (tbldept, tblcourse, tblstud, tblviolation) and I want to extract specific values. The below tables is like the same on my tables
tbldept
id | dept
1 | deptA
2 | deptB
tbldept has foreign key on tblcourse
tblcourse
id | deptId | course
1 | 2 | courseA
2 | 1 | courseB
3 | 1 | courseC
tblcourse has foreign key on tblstud
tblstud
id | courseId | name
1 | 1 | studA
2 | 2 | studB
3 | 1 | studC
tblstud has foreign key on tblviolation
tblviolation
id | studId | violationName
1 | 3 | violationA
2 | 2 | violationB
3 | 1 | violationC
4 | 3 | violationC
*What I want to get is look like this: *
dept | studId | name | violationName
2 | 1 | studA | violationC
2 | 2 | studB | violationB
1 | 3 | studC | violationA
1 | 3 | studC | violationC
I want to get all the rows of tblviolation for each studId.
I hope you guys understand what I am trying to explain. =) Thank you.

You just need inner join. Try this.
select d.dept,s.studid,s.name,v.violationname
from tbldept d
inner join tblcourse c
on d.id=c.deptid
inner join tblstud s
on c.id=s.courseid
inner join tblviolation v
on s.id=v.studid

Related

Select rows that has common relationship

I have the following table:
Book
| id | book_title |
|----|------------|
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
Teacher:
| id | teacher_name |
|----|--------------|
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
Education stage:
| id | education_stage_name |
|----|----------------------|
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
The relationship between book and education stage:
| book_id | education_stage_id |
|---------|--------------------|
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
And the relationship between book and teacher:
| book_id | teacher_id |
|---------|------------|
| 1 | 1 |
| 1 | 4 |
| 2 | 3 |
| 3 | 4 |
Now I want to select books, which has the same education stages as a teacher with id=1
So the result table should look like this:
| book_id | title |
|---------|-------|
| 1 | a |
Because only the education stage with id=1 is assigned to the book and the teacher with id=1 at the same time.
Here is the fiddle, but I don't know even how to start with this query.
Is there anyone that has some ideas on how to write this query?
Is it possible to write it with DQL language?
This doesn't take anything complex, if all records will always exist, you can inner join on all tables:
select
Book.id as book_id,
Book.book_title,
Teacher.id as teacher_id,
Teacher.teacher_name
from Book
inner join book_education_stage bes on bes.book_id = Book.id
inner join Education_Stage es on es.id = bes.education_stage_id
inner join teacher_education_stage tes on tes.book_id = Book.id
inner join Teacher on tes.teacher_id = Teacher.id
Result:
book_id
book_title
teacher_id
teacher_name
1
a
1
a
1
a
1
a
1
a
4
d
1
a
4
d
2
b
3
c
3
c
4
d
Here is the DB Fiddle if you're interested.

mySQL SELECT query from multiple tables

I need a help with mySQL SELECT query from multiple tables. I have got four tables: school, discipline, pupils and teams.
School table looks like:
+------+---------+---------------+----------+
| id | name | discipline_id | pupil_id |
+------+---------+---------------+----------+
| 1 | one | 2 | 5 |
+------+---------+---------------+----------+
| 2 | two | 3 | 8 |
+------+---------+---------------+----------+
| 3 | three | 4 | 12 |
+------+---------+---------------+----------+
Discipline table looks like:
+------+---------+
| id | name |
+------+---------+
| 1 | math |
+------+---------+
| 2 | bio |
+------+---------+
| 3 | liter |
+------+---------+
| 4 | geo |
+------+---------+
Teams table looks like:
+------+---------+---------------+-----------+
| id | name | school_id | member_id |
+------+---------+---------------+-----------+
| 1 | T1 | 1 | 3 |
+------+---------+---------------+-----------+
| 2 | T2 | 3 | 3 |
+------+---------+---------------+-----------+
| 3 | T3 | 2 | 9 |
+------+---------+---------------+-----------+
The result of disciplines I need to get with a "SELECT from discipline..." query by "member_id = 3" is:
+-----------------+---------------+
| discipline_name | discipline_id |
+-----------------+---------------+
| bio | 2 |
+-----------------+---------------+
| geo | 4 |
+-----------------+---------------+
By matching member's school and then getting its discipline, if it makes sense...Is it possible to do with just one mySQL query?
Type of: member_id 3 => school_id 1,3 => discipline_id = show related disciplines names and ids which are 2, 4
Thank you very much...
Your goal is not clear or makes no sense to me.
But here is what you are literally asking for:
SELECT
s.discipline_id
d.name
FROM teams t
LEFT JOIN school s
ON s.id = t.school_id
LEFT JOIN discipline d
ON d.id = s.discipline_id
WHERE t.member_id = 3

mysql - Limit a query with left join

I have 2 tables like this:
Table person
id | name
---------
1 | john
2 | mike
3 | carl
4 | keny
5 | anna
Table vehicle
owner | vechicle
----------------
1 | RTA457
3 | GSW684
3 | GKI321
3 | SNE798
5 | YTT662
So, I want to make a query joining both tables, something like this:
SELECT * FROM person LEFT JOIN vehicle ON person.id=vehicle.owner
Getting these results
id | name | owner | vechicle
----------------------------
1 | john | 1 | RTA457
2 | mike | NULL | NULL
3 | carl | 3 | GSW684
3 | carl | 3 | GKI321
3 | carl | 3 | SNE798
4 | keny | NULL | NULL
5 | anna | 5 | YTT662
Finally, I want to limit it to 3 persons, showing all their vehicles, like this:
id | name | owner | vechicle
----------------------------
1 | john | 1 | RTA457
2 | mike | NULL | NULL
3 | carl | 3 | GSW684
3 | carl | 3 | GKI321
3 | carl | 3 | SNE798
There is any way to do it?
May help with a subquery
SELECT
*
FROM
(SELECT * FROM person LIMIT 3) t
LEFT JOIN vehicle ON t.id = vehicle.owner
Didn't try it, but something like this:
SELECT * FROM person
LEFT JOIN vehicle ON person.id = vehicle.owner
WHERE person.id IN (SELECT ID FROM PERSON LIMIT 3);
You could simply have your query as such:
SELECT * FROM person LEFT JOIN vehicle ON person.id=vehicle.owner LIMIT 10;
This SO could be handy as well. Hope this helps!

Multiple Select Join query with corresponding column on one table to others

Lets say that I have something table like this:
<b>Name</b>
+---------+--------+
| name_id | name |
+---------+--------+
| 5 | Betti |
| 6 | Derry |
| 7 | Alfred |
| 8 | Elsie |
| 9 | Cinta |
+---------+--------+
<b>Goods</b>
+----------+-----------+
| goods_id | goods |
+----------+-----------+
| 1 | Computer |
| 2 | AC |
| 3 | Microwave |
| 4 | TV |
+----------+-----------+
<b>Transaction</b>
+-------+---------+----------+
| ai_id | name_id | goods_id |
+-------+---------+----------+
| 1 | 7 | 2 |
| 2 | 5 | 4 |
| 3 | 9 | 3 |
+-------+---------+----------+
I want to replace name_id column on Transaction table with name column on Name table with corresponding name_id column and so for goods_id to produce something similar to this table:
<b>Transaction</b>
+-------+--------+-----------+
| ai_id | name | goods |
+-------+--------+-----------+
| 1 | Alfred | AC |
| 2 | Betti | TV |
| 3 | Cinta | Microwave |
+-------+--------+-----------+
If you're looking to just display the information rather than actually "replacing" your tables with new ones, then you could use a SELECT query with a simple INNER JOIN. This way you can display columns from multiple tables.
SELECT T.ai_id, N.Name, G.goods
FROM Transaction T
INNER JOIN Name N ON N.name_id = T.name_id
INNER JOIN Goods G ON G.goods_id = T.goods_id;
Try with inner join
SELECT T.ai_id,N.name,G.goods
FROM Transaction as T
INNER JOIN Goods as G ON T.goods_id = G.goods_id
INNER JOIN Name as N ON N.name_id = T.name_id;
Try this one
select tb3.ai_id,tb2.name,tb1.goods from Goods tb1,Name tb2,Transaction tb3 where tb3.name_id=tb2.name_id and tb3.goods_id=tb1.goods_id order by tb3.ai_id
try this:
SELECT C.ai_id,A.name,B.goods
FROM Transaction C
INNER JOIN Name A
ON A.name_id=C.name_id
INNER JOIN Goods B
ON B.goods_id=C.goods_id;
http://sqlfiddle.com/#!2/3c5f3/8

multilevel mysql query with a weird setup

I have a problem. I am a codding newbie and learning php and mysql as i go.
i am working on an application and ran into a problem that i cant figure out.
I have three tables:
category:
+-----------+----------+--------------------+
| id | name | sort | active |
+-----------+----------+--------------------+
| 1 | apple | 1 | 1 |
| 2 | mango | 2 | 1 |
| 3 | blueberry| 3 | 0 |
+-----------+----------+--------------------+
service :
+-----------+----------+----------------------------------------+
| id | category_id | name | sort |active| process_time |
+-----------+------+------+--------+------+------+--------------+
| 1 | 1 | slices | 1 | 1 | 2 hours |
| 2 | 1 | half | 2 | 1 | 6 hours |
| 3 | 2 | slices | 1 | 1 | 1 hour |
| 4 | 2 | whole | 2 | 1 | 6 hours |
| 5 | 3 | bunch | 1 | 0 | 12 hours |
| 6 | 3 | a lot | 2 | 1 | 10 hours |
+-----------+------+---------+------+-------------+-------------+
user_price:
+-----------+----------+------+-------+---------+---------+---------+---------+
| id | user_id | 1 | 2 | 3 | 4 | 5 | 6 |
+-----------+----------+------+-------+---------+---------+---------+---------+
| 1 | 20 | 1 | 1 | 5 | 4 | 6 | 6 |
| 2 | 22 | 2 | 1 | 2 | 3 | 5 | 1 |
| 3 | 32 | 3 | 2 | 7 | 4 | 1 | 3 |
+-----------+----------+------+-------+---------+---------+---------+---------+
Categories have the categories that i sell in my store.
Services have the services that i offer in my store they correspond to the categories and are unique to only that category.
Prices have the user prices that i charge my customers every user has a different price the column names in the price table correspond to the services id.
Here is a SQLFiddle with sample data
Now here is the problem, in one sweep i would like to get a row that contains columns from all three tables for user_id 22 (user_id is only used for prices)
+---------------+----------------+-----------------+------------------+-----------+
| service_id | category_name | service_name | process_time | price |
+---------------+----------------+-----------------+------------------+-----------+
| 3 | mango | slices | 1 hour | 2 |
+---------------+----------------+-----------------+------------------+-----------+
Any ideas???
Try
SELECT s.id service_id,
c.name category_name,
s.name service_name,
s.process_time,
p.`3` price
FROM service s JOIN category c
ON s.category_id = c.id JOIN user_price p
ON p.user_id = 22
WHERE s.id = 3
Output:
| SERVICE_ID | CATEGORY_NAME | SERVICE_NAME | PROCESS_TIME | PRICE |
--------------------------------------------------------------------
| 3 | mango | slices | 1 hour | 2 |
Here is SQLFiddle demo
Now while it's not too late please normalize your user_price table. That will save you a lot of headache in the long run.
Your renewed schema for this table might look like
CREATE TABLE IF NOT EXISTS user_price
(
id int(11) NOT NULL AUTO_INCREMENT,
user_id int(11) NOT NULL,
service_id int(11) NOT NULL,
price decimal(19,2) DEFAULT '0.00',
PRIMARY KEY (id),
FOREIGN KEY (service_id) REFERENCES service (id),
UNIQUE KEY (user_id, service_id)
);
And the proper query using this table
SELECT s.id service_id,
c.name category_name,
s.name service_name,
s.process_time,
p.price
FROM service s JOIN category c
ON s.category_id = c.id JOIN user_price p
ON s.id = p.service_id
AND p.user_id = 22
WHERE s.id = 3
Here is a SQLFiddle demo. In this example the table has been named user_price1 to show you both queries side-by-side.