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.
Related
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'
How to write this query in laravel 5.1
SELECT
(SELECT firstname FROM registration_details a INNER JOIN users b
ON a.id = b.registerid
WHERE c.patientid = b.id ) AS patient,
(SELECT firstname FROM registration_details a INNER JOIN users b
ON a.id = b.registerid
WHERE c.doctorid = b.id ) AS doctor,
c.`appoinmentdate`
FROM `appoinments` c
I tried like this But i get Undefined property: stdClass::$id error
DB::table('appoinments')
->select(DB::raw(
"(SELECT firstname FROM registration_details INNER JOIN users ON registration_details.id = users.registerid WHERE appoinments.patientid = users.id) AS patient",
"(SELECT firstname FROM registration_details INNER JOIN users ON registration_details.id = users.registerid WHERE appoinments.doctorid = users.id) As doctor",
"appoinments.appoinmentdate",
"appoinments.id",
"(SELECT timings FROM appoinment_time WHERE appoinment_time.id = appoinments.appoinment_time) AS apptime ",
"(SELECT branchname FROM branches WHERE branches.id = appoinments.branchcode) AS branch"))
->get();
Finally got the answer. Thanks to all who try for me.
$appoinment = DB::table('appoinments')->select(DB::raw("(SELECT firstname FROM registration_details INNER JOIN users ON registration_details.id = users.registerid WHERE appoinments.patientid = users.id) AS patientid"));
$appoinment = $appoinment->addSelect(DB::raw("(SELECT firstname FROM registration_details INNER JOIN users ON registration_details.id = users.registerid WHERE appoinments.doctorid = users.id) As doctorid"));
$appoinment = $appoinment->addSelect(DB::raw("appoinments.appoinmentdate"));
$appoinmentlist = $appoinment3->addSelect(DB::raw("appoinments.id"))->get();
In a table if I have:
FixtureID, HomeTeam, AwayTeam
Can I replace the ID that is related to HomeTeam and AwayTeam when it displays in the browser? HomeTeam and AwayTeam are both related to TeamID in the Teams table.
I want to show all fixtures and then replace both "TeamID" with "TeamName" so the name shows up instead of the ID?
So far I have:
$sql = <<<SQL
SELECT fix.*, tea.*
FROM Fixtures fix
INNER JOIN Teams tea USING (TeamID)
SQL;
Then
echo '<p>Fixtures</p>';
echo '<div>'.$row['HomeTeam'].' v '.$row['AwayTeam'].'</div>';
EDIT:
Ok so I found a post that was similar to what I need and have tried the following:
$sql = <<<SQL
SELECT fix.*, tea1.*, tea2.*
FROM Fixtures fix
INNER JOIN Teams tea1 ON fix.HomeTeam = tea1.TeamID
INNER JOIN Teams tea2 ON fix.AwayTeam = tea2.TeamID
SQL;
However it's still just showing the team ID's rather than the names from the Teams table.
Solved it myself
$sql = <<<SQL
SELECT f.*, t1.TeamName 'HomeTeam', t2.TeamName 'AwayTeam'
FROM Fixtures f
INNER JOIN Teams t1 ON f.HomeTeam = t1.TeamID
INNER JOIN Teams t2 ON f.AwayTeam = t2.TeamID
SQL;
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.
table user:
id_u* f_name l_name
----------------------
1 andi mitchel
2 sarah bench
3 kirsty larx
table voucher:
id_v* id_user id_target
1 1 2
2 2 3
quite confused how to join those table with two foreign keys
$db->query("SELECT * FROM voucher v
LEFT JOIN user u ON u.id_u = v.id_user
LEFT JOIN user u1 ON u1.id_u = v.id_target
WHERE .... ")
echoing while loop... and returns nothing??
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
echo $r['u.f_name'];
echo $r['u1.f_name'];
endwhile;
Your JOIN seems absolutely correct. The only issue is that you have joined table user twice, therefore you have columns with same name (like f_name). The database will assign different (but arbitrary) names to these columns. You can override this behaviour with the AS keyword:
$db->query("SELECT v.*
, u.f_name AS user_f_name
, u.l_name AS user_l_name
, ta.f_name AS target_f_name
, ta.l_name AS target_l_name
FROM voucher v
LEFT JOIN user u ON u.id_u = v.id_user
LEFT JOIN user ta ON ta.id_u = v.id_target
WHERE .... ")
Then:
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
echo $r['user_f_name'];
echo $r['target_f_name'];
endwhile;
And I think you can replace the LEFT JOINs with (inner) JOINs. Unless you have id_user or id_target values referencing non-existing userids (id_u).
It looks like you are asking for all people who are in the voucher table regardless of them being in position 1 (user) or position 2 (target)... Then, showing that person's name.
This query does a pre-query of each possible person and their position basis (via WhichPosition).
SELECT STRAIGHT_JOIN
AllVoucherUsers.WhatPosition,
u.*
FROM
( select distinct
v.id_user,
'1' as WhatPosition
from voucher v
union select distinct
v.id_target as id_user,
'2' as WhatPosition
from voucher v
) AllVoucherUsers
join users u
on AllVoucherUsres.id_user = u.id_u
If you only want ONE instance of a given person -- REGARDLESS of their position, just strip out all instances of the "WhatPosition" reference...
SELECT STRAIGHT_JOIN
u.*
FROM
( select distinct
v.id_user
from voucher v
union select distinct
v.id_target as id_user
from voucher v
) AllVoucherUsers
join users u
on AllVoucherUsres.id_user = u.id_u
SELECT * FROM voucher v
LEFT JOIN user u ON u.id_u = v.id_user OR u.id_u = v.id_target
WHERE ....
how about:
SELECT * FROM voucher JOIN user ON id_user = id_u
Simpler still:
SELECT * FROM voucher, user WHERE id_user = id_u