table 1 name : nfc_film
Column Name = id, film_id, title, description
table 2 name : nfc_film_actor
Column name = id, actor_id, film_id
table 3 name : nfc_actor
Column name = actor_id, first_name, last_name
Question : How can I join this 3 table together?
I want get the value first_name and last_name from table nfc_actor but my query doesn't work.
My query:
SELECT *
FROM nfc_film
INNER JOIN nfc_film_actor ON
nfc_film_actor.film_id = nfc_film.film_id
INNER JOIN nfc_actor ON
nfc_actor.actor_id = nfc_film_actor.actor_id
WHERE nfc_film.film_id = :filmid
You cannot use the * selector in your query, because id, actor_id and film_id exist in multiple tables, this definitely wrong.
You should refer to the specific name of each field.
If you check your web-server error.log you can see that.
Maybe did you mean:
SELECT first_name, last_name FROM nfc_film
INNER JOIN nfc_film_actor
ON nfc_film_actor.film_id = nfc_film.film_id
INNER JOIN nfc_actor
ON nfc_actor.actor_id = nfc_film_actor.actor_id
WHERE nfc_film.film_id = nfc_film_actor.film_id
use this
SELECT nfc_actor.first_name,nfc_actor.last_name,
FROM nfc_film
INNER JOIN nfc_film_actor
ON nfc_film_actor.film_id = nfc_film.film_id
INNER JOIN nfc_actor
ON nfc_actor.actor_id = nfc_film_actor.actor_id
WHERE nfc_film.film_id = 'some_id';
give id of film in the place of 'some_id'
Related
I have a table_resident like in image below
And then i have a table_complaints like image below
with the table_resident.resident_id = table_complaints.resident_id and also table_resident.resident_id = table_complaints.party_id
How can i join resident_id = 1 and party_id = 2 name in same row like image below? I can see resident_id = 1 first_name, last_name but in resident_id = 2 how can i see them again by using party_id?
Here is my code in joining but in party_id i dont know what i will use the firstname, lastname, middlename again
SELECT
table_involvement.*,
table_resident.first_name,
table_resident.last_name,
table_resident.middle_name,
table_complaints.*
FROM ((
table_complaints
LEFT JOIN table_resident
ON table_complaints.resident_id = table_resident.resident_id)
LEFT JOIN table_involvement
ON table_complaints.complaints_id = table_involvement.complaints_id)
ORDER BY table_resident.first_name ASC";
This should do it. I user 2 inner joins to achieve this solution.
SELECT TABLE_RESIDENT.FIRST_NAME, TABLE_RESIDENT.LAST_NAME,
TABLE_RESIDENT.MIDDLE_NAME, TABLE_COMPLAINTS.*, TABLE_INVOLVEMENT.*
FROM TABLE_RESIDENT
INNER JOIN TABLE_COMPLAINTS
ON TABLE_COMPLAINTS.RESIDENT_ID = TABLE_RESIDENT.RESIDENT_ID
INNER JOIN TABLE_INVOLVEMENT
ON TABLE_COMPLAINTS.RESIDENT_ID = TABLE_INVOLVEMENT.COMPLAINTS_ID
WHERE TABLE_COMPLAINTS.PARTY_ID = 2
ORDER BY TABLE_RESIDENT.FIRST_NAME ASC
I have a two different tables
Table 1
id
name
description
Table 2
id
details
info
table1_id
I want to display all the records from the table1 except id but from table2 I used to display the max id.
eg. table1 have following records
id=1
name = test
description = some text
table2 have
id=5
details = some more text
info = the new info
table1_id = 1
so the result what I want is
id name description
5 test some text
Try this:
select
(select max(table2.id) from table2 where table1.id = table2.table1_id) id,
name,
description
from table1
or left join:
select
t.id,
table1.name,
table1.description
from table1
left join (
select max(id) id, table1_id from table2 group by table1_id
) t on table1.id = t.table1_id
You can try with and max.
with ID_Table_1_MaxID_Table_2 as (
select table1_id, max(id) Max_Table2_ID
from Table_2
group by table1_id
)
SELECT tb2.id, tb1.name, tb1.description
FROM Table_2 tb2
INNER JOIN ID_Table_1_MaxID_Table_2 sub
ON (sub.table1_id = tb2.table1_id and tb2.id = sub.Max_Table2_ID)
INNER JOIN Table_1 tb1 on tb1.id = sub.table1_id
i want to retrieve Data form 3 specifies Tables namely
UserDetail(Fname,Lname,User_id),
Movies(Movie_id,MovieName)
UserLikedMovies(User_id,Movie_id)
such that when a user enter a specific Movie_id then Userid Fname Lname form User detail MovieName from Movies,,
Here is what i tried
SELECT UserDetail.FName
FROM
UserDetail UserDetail
INNER JOIN
UserLikedMovies UserLikedMovies
ON
UserDetail.User_id = UserLikedMovies.User_id
INNER JOIN
(
SELECT
Movies.MovieName,
Movies.Movie_id
FROM
Movies Movies
INNER JOIN
UserLikedMovies UserLikedMovies
ON
Movies.Movie_id = UserLikedMovies.Movie_id
INNER JOIN
UserDetail UserDetail
ON
UserLikedMovies.User_id = UserDetail.User_id
WHERE
Movies.Movie_id IN ( Select UserLikedMovies.Movie_id from UserLikedMovies where UserLikedMovies.Movie_id = 4)
) as ABC
ON UserLikedMovies.Movie_id = ABC.Movie_id
AND Movies.Movie_id = ABC.Movie_id
Suppose movie_id is 9, then query is below.
SELECT User.Fname, User.Lname, (SELECT MovieName FROM Movies WHERE Movie_id = 9) AS MovieName
FROM UserDetail User
INNER JOIN UserLikedMovies Like
ON User.User_id = Like.User_id
WHERE Like.Movie_id = 9;
It is pretty simple query:
SELECT ud.`User_id`, ud.`Fname`, ud.`Lname`, m.`MovieName`
FROM `Movies` m
RIGHT JOIN `UserLikedMovies` ulm ON ulm.`Movie_id` = m.`Movie_id`
LEFT JOIN `UserDetail` ud ON ud.`User_id` = ulm.`User_id`
WHERE m.`Movie_id` = 4
I do not know why you are using fearful sub-queries for this simple task.
I have about six(6) tables each linked with userid. One of the tables is userinfo. The user info contains user details including their store platform(eg magento)
Userinfo contains both active and non-active users (active users have created at least one activity in the other 5 tables).
I want to count distinct number of users in the userinfo with platform of magento who have records in any of the other tables.
Currently I am able to count distinct number of users in the other five tables with the ff code but want to join this with the userinfo table so I can select active users with platform magento.
Without adding the userinfo table means I have no way of selecting users by platform.
Selecting users in userinfo table only, with platform of magento will be easy, but that means I may select users who only register but do not go on to create activity on my app.
$query3 = ("SELECT COUNT(*)
FROM (
SELECT userid FROM table1
UNION SELECT userid FROM table2
UNION SELECT userid FROM table3
UNION SELECT userid FROM table4
UNION SELECT userid FROM table5
) AS UserIDs");
$result3 = mysql_query($query3) or die(mysql_error());
$row3 = mysql_fetch_row($result3);
echo "Number of distinct users in all tables = ".$row3[0] ."<br />";
**Table 1**
Id userid name adresss
**Table 2**
Id Title Sex userid
**Table 3**
Id userid amount
**Table 4**
Id price promotion userid productid
**Table 5**
Id userid category tax weight
**userinfo**
Id userid username password platform
Expanding on the UNION subselect from my other suggestion, you can JOIN this with the UserInfo table and get your distinct count.
SELECT COUNT (DISTINCT ui.UserID))
FROM (
SELECT UserID FROM Table1
UNION SELECT UserID FROM Table2
UNION SELECT UserID FROM Table3
UNION SELECT UserID FROM Table4
UNION SELECT UserID FROM Table5
) AS id
INNER JOIN UserInfo ui ON ui.UserID = id.UserID
WHERE ui.Platform = 'Magento'
I would like that :
SELECT COUNT(DISTINCT ui.userid) as number
FROM userinfo ui
INNER JOIN table1 t1 ON (t1.userid = ui.userid)
INNER JOIN table2 t2 ON (t2.userid = ui.userid)
INNER JOIN table3 t3 ON (t3.userid = ui.userid)
INNER JOIN table4 t4 ON (t4.userid = ui.userid)
INNER JOIN table5 t5 ON (t5.userid = ui.userid)
WHERE ui.platform = 'magento'
And if you do :
SELECT COUNT(DISTINCT ui.userid) as number
FROM userinfo ui, table1 t1, table2 t2, table3 t3, table4 t4, table5 t5
WHERE ui.platform = 'magento'
AND t1.userid = ui.userid
AND t2.userid = ui.userid
AND t3.userid = ui.userid
AND t4.userid = ui.userid
AND t5.userid = ui.userid
If it doesn't work, try to replace SELECT COUNT(DISTINCT ui.userid) as number by SELECT ui.* for see.
I am struggling with a MYSQL query - I have 2 tables :
Table 1 (info) containing UID, first_name, last_name.
Table 2 (card) containing UID, pic .
What I am trying to do is get all results into an array:
WHERE UID IN '$ids' AND LEFT(last_name,1) = '$letter' ORDER BY last_name, first_name ASC
I figured an INNER JOIN so my current code is:
("SELECT UID, first_name, last_name, pic FROM
(SELECT info.first_name,info.last_name,card.pic FROM info
INNER JOIN card ON info.UID=card.UID)
WHERE LEFT(last_name,1) = '$letter' ORDER BY last_name, first_name ASC")
This is producing the following error though:
'Every derived table must have it's own alias'.
Am I going about this the right way with inner join, and how do I give the derived table an alias? Thanks in advance!
select b.UID, g.first_name, g.last_name, b.pic
from user_data.general_info g
inner join user_data.Bcards b on g.UID = b.UID
where LEFT(g.last_name, 1) = '$letter'
order by g.last_name, g.first_name asc
The inner query should be named.
SELECT users.UID, users.first_name, users.last_name, users.pic FROM
(SELECT info.first_name,info.last_name,card.pic FROM user_data.general_info
INNER JOIN user_data.Bcards ON general_info.UID=Bcards.UID) users
WHERE LEFT(users.last_name,1) = '$letter' ORDER BY users.last_name, users.first_name ASC