hellow.I have 3 table like below;
Movie Table like this:
+------+-------+-----+
| id | name | |
+------+-------+-----+
+------+-------+-----+
| 1 | got | |
+------+-------+-----+
+------+-------+-----+
| 2 | Drive | |
+------+-------+-----+
+------+-------+-----+
| 3 | Thor | |
+------+-------+-----+
Janra:
+------+-------------+
| id | janra_name |
+------+-------------+
+------+-------------+
| 1 | action |
+------+-------------+
+------+-------------+
| id | comedy |
+------+-------------+
Movie_janra: (movie_id is a foreign key to id in movie table)
+--------+-------------+
| mov_id | janra_id |
+--------+-------------+
+--------+-------------+
| 1 | 1 |
+--------+-------------+
+--------+-------------+
| 1 | 2 |
+--------+-------------+
nation Table:
+------+-------------+
| id | name |
+------+-------------+
+------+-------------+
| 1 | us |
+------+-------------+
+------+-------------+
| 2 | uk |
+------+-------------+
movie_nation Table:
+------+-------------+
|mov_id| nation_id |
+------+-------------+
+------+-------------+
| 1 | 1 |
+------+-------------+
+------+-------------+
| 1 | 2 |
+------+-------------+
then i use query like this:
SELECT *
FROM
(SELECT movie.id,
movie.name,
f.janra_name
FROM `movie`
LEFT JOIN
(SELECT mj.movie_id,
janra.janra_name
FROM movie_janra AS mj
LEFT JOIN janra ON mj.janra_id=janra.id) AS f ON f.movie_id=movie.id) AS ll
LEFT JOIN
(SELECT movie_nation.movie_id,
nation.nation
FROM movie_nation
INNER JOIN nation ON nation.id=movie_nation.nation_id) AS rr ON rr.movie_id=ll.id
(join movie with result of (movie_janra and janra)) AS ll this give me all movies with all janra like below
id name janra
1 got action
1 got comedy
then i join this with result of (join movie_nation with nation) AS rr ON rr.movie_id = ll.id
but this query result is like this
+-----------+-------------+-------------+----------+
| id | name | janra | nation |
+-----------+-------------+-------------+----------+
+-----------+-------------+-------------+----------+
| 1 | got | action | us |
+-----------+-------------+-------------+----------+
+-----------+-------------+-------------+----------+
| 1 | got | action | uk |
+-----------+-------------+-------------+----------+
+-----------+-------------+-------------+----------+
| id | name | comedy | us |
+-----------+-------------+-------------+----------+
+-----------+-------------+-------------+----------+
| id | name | comedy | uk |
+-----------+-------------+-------------+----------+
I use MySQL.
This result is more than I want. I want two row. Thank you all.
First thing, remove the tables movie_nation and movie_janra then just move the columns to movie table as shown below then retain all the other tables:
Database tables
4: https://i.stack.imgur.com/Mf6MU.png
then I have added table data:
movie table
Janra table
Nation table
Then use this code to get your desired output:
select a.id,a.movie_name,b.janra_name, c.nation_name
from movie a
left outer join janra b
on a.janra_id = b.id
left outer join nation c
on a.nation_id = c.id;
Just select the ID and Movie name to movie table then left outer join to nation and janra table using their corresponding IDs from table movie to get the janra_name and nation_name.
This is the ouput of it: query output here
based on the previous answer, you just need to change the data type of janra_id and nation_id from movie table from int to varchar. then you can now use multiple janras and nation using comma separated values.
use this new code to achieve that.
select a.id, a.movie_name, GROUP_CONCAT(distinct a.janra_name) janra_name,
GROUP_CONCAT(distinct a.nation_name) nation_name
from (
select a.*,b.janra_name,c.nation_name
from movie a
left outer join janra b
on FIND_IN_SET(b.id, a.janra_id)
left outer join nation c
on FIND_IN_SET(c.id, a.nation_id)
order by a.movie_name ) a
group by a.movie_name;
Here is the sample data:
And here is the output of the query:
Related
i have 2 table like this
student
|-----------------------|
| id | name | value |
|-----------------------|
| F01 | Ruben | 4 |
| F02 | Dani | 2 |
| F03 | Mike | 3 |
| F04 | John | 4 |
|-----------------------|
tutor
|-------------------------|
| id | code | student_id |
|-------------------------|
| 1 | S2244 | F01 |
| 2 | S3251 | F02 |
| 3 | S2244 | F03 |
| 4 | S2244 | F04 |
|-------------------------|
note, tutor.code ( S2244 and S3251) is foreign key from another table, tutor.student_id is foreign key from student table, how to make the two tables combined and produce a result as below?
|-----------------------|
| id | name | value |
|-----------------------|
| F01 | Ruben | 4 |
| F03 | Mike | 3 |
| F04 | John | 4 |
|-----------------------|
the result is the same as the student table, but the data is released based on what is stored in the tutor table, in the tutor table there is code "S3251" / "F02" which is not displayed in the results table
this is like the "WHERE" condition but the WHERE 'condition ' is used in other tables, I've tried using JOIN but i can't, or maybe my table design is wrong? Please help, this code that I made but didn't get a good result
SELECT st.id, st.name, st.value FROM student st JOIN tutor tt ON tt.code = 'S2244'
You can use exists:
select s.*
from students s
where exists (
select 1 from tutor t where t.student_id = s.student_id and t.code = 'S2244'
)
This makes more sense than a join, since you are not selecting from the tutor table).
The problem with your original attempt is that you are missing a joining condition on student_id (which is common to both tables). The syntax would be:
select s.*
from student s
inner join tutor t on s.id = t.student_id
where t.code = 's2244'
In addition to #GMB's answer above, which is absolutely correct, you could also do the following, which I think might be more intuitive for a beginner developer:
select * from student
where id in (
select student_id from tutor where id = 's2244'
)
I am working on a music festival app. I have three tables, artists, vehicles, and vehicleLink.
artists is;
| id | name |
|----|----------|
| 1 | Artist 1 |
| 2 | Artist 2 |
| 3 | Artist 3 |
vehicles is;
| id | type | reg |
|----|------|------|
| 1 | Car | REG1 |
| 2 | Van | REG2 |
| 3 | Car | REG3 |
vehicleLink is;
| id | artist_id | vehicle_id |
|----|-----------|------------|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 3 |
I can easily show lists of all artists vehicles with the following MySQL;
SELECT v.id, v.type, v.reg, a.name AS artist_name
FROM vehicles v
JOIN vehicleLink l ON v.id = l.vehicle_id
JOIN artists a ON l.artist_id = a.id
This gives;
| id | reg | type | artist_name |
|----|------|------|-------------|
| 1 | REG1 | Car | Artist 1 |
| 2 | REG2 | Van | Artist 1 |
| 3 | REG3 | Car | Artist 2 |
I now need a list where artist_id IS NOT present in vehicleLink yet. e.g to print a list of people who have yet to give us their vehicle details.
I've scoured StackOverflow and Google but not managed to find anything that worked, I'm just trying this directly in phpMyAdmin. For example adding the below WHERE NOT, returns 0 rows;
where not exists (select 1 from artists where id = l.artist_id)
Table artists has id.
Table vehicleLink has artist_id
I need the right query to show which artists.id does not appear in vehicleLink, which in these examples is Artist 3
you should try using not in the id with vehicles
select a.name AS artist_name, a.id AS artist_id
from artists a where a.id not IN (
SELECT a.id
FROM vehicles v
JOIN vehicleLink l ON v.id = l.vehicle_id
JOIN artists a ON l.artist_id = a.id
)
I have
jobs table with fields
jobId, jobTitle, jobDesc
jobQuotes Table with fields
id, user_id, quote
jobQuotes table has the quote of users who gave quote for the job.
I need those jobs for which a specific user has NOT given any quote.
Using LEFT JOIN I get all the jobs irrespective of the jobQuotes table.
And INNER JOIN only gives all the jobs that has a relevant jobQuote.
But I need those jobs for which a specific user has NOT given any quote.
My Query is
SELECT * FROM dummy_jobs J LEFT JOIN jobQuotes JQ ON J.jobId=JQ.jobId WHERE MATCH (J.jobTitle, J.jobDescription) AGAINST ('php, mysql');
How to filter this result set so that output doesn't have specific user_id in jobQuotes?
SELECT jobstable.jobid from jobstable inner join
(SELECT id from jobQuotes where userid = 953 and quote IS NULL) dummy_table
on dummy_table.id == jobstable.jobid;
The Answer is According to the comment you were given
"I want all jobs for which userId= 953 has not given any quote"
An approach might be to associate a specific user with all jobs by using a cross join and then left join to job quotes with a null test to find those not quoted for.
for example
users
+----+----------+
| id | username |
+----+----------+
| 1 | John |
| 2 | Jane |
| 3 | Ali |
| 6 | Bruce |
| 7 | Martha |
+----+----------+
Jobs
+-------+----------+---------+
| jobId | jobTitle | jobDesc |
+-------+----------+---------+
| 1 | a | a |
| 2 | b | b |
| 3 | c | c |
+-------+----------+---------+
Jobquotes
+------+---------+-------+
| id | user_id | quote |
+------+---------+-------+
| 1 | 3 | 10 |
| 2 | 2 | 10 |
+------+---------+-------+
select t.id,t.username,t.jobid,t.jobtitle,t.jobdesc
from
(
select u.id,u.username, s.jobid,s.jobtitle,s.jobdesc
from users u
cross join (select distinct jobid , jobtitle, jobdesc from jobs) s
where u.id = 3
) t
left join jobquotes jq on jq.id = t.jobid and jq.user_id = t.id
where jq.id is null
result
+----+----------+-------+----------+---------+
| id | username | jobid | jobtitle | jobdesc |
+----+----------+-------+----------+---------+
| 3 | Ali | 2 | b | b |
| 3 | Ali | 3 | c | c |
+----+----------+-------+----------+---------+
I'm confused using a JOIN - Statement to select my data from my MySQL Database.
Here is how the tables look like:
Table "Users"
+----+-------+----------+
| ID | Name | Password |
+----+-------+----------+
| 1 | Mike | test |
| 2 | Tony | test1 |
| 3 | Frank | test2 |
+----+-------+----------+
Table "Games"
+----+-----------+-----------+---------+---------+
| ID | Player1ID | Player2ID | ScoreP1 | ScoreP2 |
+----+-----------+-----------+---------+---------+
| 1 | 1 | 2 | 5 | 2 |
| 2 | 3 | 1 | 2 | 1 |
+----+-----------+-----------+---------+---------+
I would like to SELECT * FROM GAMES WHERE Player1ID=1 or Player2ID=1 plus the names of the users and not just their IDs.
Can someone help me with that?
Join the users table twice with different alias names for them to distinguish them
select g.*, u1.name as player1, u2.name as player2
from games g
join users u1 on u1.id = g.player1id
join users u2 on u2.id = g.player2id
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