Joining 2 fields into another table - php

How do i join two fields from table1 to table 2?
I have this code below, how do i also join fields "mobilenumber","firstname" and "lastname" from the user table into the user_address table?
$query = "SELECT * FROM user_address WHERE user_id IN
(SELECT id FROM user WHERE email = '".$email."')";

$query = "SELECT a.*,b.mobilenumber,b.firstname,b.lastname
FROM user_address a
left join user b on a.user_id=b.user_id
WHERE a.user_id IN
(SELECT id FROM user WHERE email = '".$email."')";

You can use LEFT JOIN instead and select attributes from both table. Example:
$query = "SELECT ua.*,
u.mobilenumber,
u.firstname,
u.lastname
FROM user_address ua
LEFT JOIN USER u
ON u.id = ua.user_id
WHERE u.email = '$email'";

Related

Specify columns in INNER JOINed table

comment table and post table both has column named user_id
I cannot specify both table's user_id
for using some if else condition later I need both the user_id as a different name (I'm trying to use AS).
I tried different way but query not working:
$sql="SELECT `post_id`, `comment_id`, `comment`, `user_id`, `username`,
`is_marked` `post`.`user_id` AS `p_uid` FROM `comment` INNER JOIN `user` ON
`comment`.`user_id` = `user`.`id` INNER JOIN `post` ON
`user`.`id`=`post`.`user_id` ORDER BY `comment_id` DESC";
$result = mysqli_query($con, $sql);
if ($result) {
while ($row=mysqli_fetch_assoc($result)) {
$post_user_id = $row['p_uid'];
You could alias table name with other name and get column. View example:
SELECT C.comment_id, U.user_id
FROM comment C INNER JOIN user u ON C.user_id = U.id
I would do:
SELECT c.post_id,
c.comment_id,
c.comment,
c.user_id AS c_uid,
c.username,
c.is_marked,
p.user_id AS p_uid
FROM comment c
INNER JOIN user u ON c.user_id = u.id
INNER JOIN post p ON c.user_id = p.id
ORDER BY c.comment_id DESC
Define aliases for the tables, and the selected fields. It makes it simpler to read than putting the table names all the time.
In your PHP you can then reference $row['c_uid'] or $row['p_uid'].

Inner join of two tables [UPDATED]

tbl teacher
lrn
fname
lname
email
image
schedule
tbl user
lrn
fname
lname
email
image
account-type
$id=$_SESSION['id'];
$get_record_sched = mysqli_query ($db, "SELECT schedule FROM teacher INNER JOIN users ON teacher.lrn = users.lrn WHERE teacher.lrn = '$id' ");
I want to echo the exact schedule based on the id of the logged in account. But with this, query, there's no output...
You said you have $id from the session. Assuming that column lrn is the ID, then
Select t.schedule
From teacher t
Where t.lrn = $id
No need to join the table with the user table.
Note that parameter binding is advised.
$statement = $connection->prepare("Select t.schedule From teacher t Where t.lrn = ?");
$statement->bind_param("s", $id);
In you where condition was wrong.They don't have any param to validate condition.so change like this
"SELECT schedule FROM teacher INNER JOIN users ON teacher.id = users.id WHERE 1 "
OR
use same where condition with join
"SELECT schedule FROM teacher INNER JOIN users ON teacher.id = users.id AND teacher.schedule = users.id WHERE 1 "
OR
if You have logged used id in session like
$id=$_SESSION['user_id'];
"SELECT schedule FROM teacher INNER JOIN users ON teacher.id = users.id WHERE teacher.schedule='$id' "

join with two columns and get field values from their foreign key with single sql query

I have two tables:
tbl_users (id,first_name,last_name,email,password)
tbl_claims (id, claimed_by, claimed_to) (both claimed_by and claimed_to is user_id of tbl_users table means foreign key)
Now how can i get the first_name and last_name of both claimed_by and claimed_to userid with single sql query?
I am trying below query but i gives unknown column error while i am using it in where clause.
SELECT c.*,
(select first_name from tbl_users where id=c.claimed_by) as claimed_by_fname,
(select last_name from tbl_users where id=c.claimed_by) as claimed_by_lname,
(select first_name from tbl_users where id=c.claimed_to) as claimed_to_fname,
(select last_name from tbl_users where id=c.claimed_to) as claimed_to_lname
from tbl_claims as c INNER JOIN tbl_users as u ON u.id = c.claimed_by INNER JOIN
tbl_users as u1 ON u1.id = c.claimed_to where claimed_by_fname like '%kotak%'
I know the above sql query is not efficient at all, i have just tried. It gives me record without where clause but does not work with where clause.
You don't need subselect just join
select c.*
, u1.first_name claimed_by_fname
, u1.last_name claimed_by_lname
, u2.first_name claimed_to_fname
, u2.last_nameclaimed_to_lname
from tbl_claims as c
INNER JOIN tbl_users as u1 ON u1.id = c.claimed_by
INNER JOIN tbl_users as u2 ON u2.id = c.claimed_to
where u1.claimed_by_fname like '%kotak%'

MySQL SELECT distinct very slow

For my mobile App i am using a web service to get the images and all records from the MySQL database, its like Instagram.
But SELECT DISTINCT taking more than 7 - 8 seconds.is there any thing that i can do for reduce the time?
SELECT distinct count(*) as totalrecord ,p.description,p.photo_id ,p.user_id,p.imagepath1 ,p.friends, p.is_report_photo,
p.imagepath2,p.imagepath3,p.imagepath4,p.imagepath5,p.count_comment,p.count_like,p.created as photo_created ,
(select username from users where id = p.user_id) as username ,
(select country from users where id = p.user_id) as country,
(select email from users where id = p.user_id) as email ,
(select image_path from users where id = p.user_id) as image_path ,
(select gender from users where id = p.user_id) as gender,
(select privacy from users where id = p.user_id) as privacy,
(select audio_privacy from users where id = p.user_id) as audio_privacy,
(select video_privacy from users where id = p.user_id) as video_privacy,
(select photo_privacy from users where id = p.user_id) as photo_privacy,
If(pl.user_id && pl.photo_id !='',like_status,0) as islike ,
If(pr.user_id && pr.photo_id !='',1,0) as isreport,
p.user_visibility
from friends as fr
inner join users as u on u.id = (select distinct if (fr.user_id != $user_id,fr.user_id,(fr.to_user_id)) as rd
from friends) && fr.read_status = '1'
inner join photos as p on (p.user_visibility = 'p')
LEFT JOIN photolike as pl ON pl.photo_id = p.photo_id && pl.user_id = $user_id
LEFT JOIN photo_report as pr on pl. photo_id = pr.photo_id && pr.user_id = $user_id
ORDER BY p.photo_id DESC;
Use mysql indexing that will reduce the time
Try using group-by clause instead of distinct.

How to display users from two MySQL tables

I have two mysql tables-members and addclique. Members table contains the details of all users while addclique table is 'friends' table with two columns-adder_id, which contains id of the user that sent a friend request and clique_id, contains the id of user that accepted the friend request.
Please am trying to select the friends of a particular user but am finding hard getting the details (photo, name, etc) of the friends from the members table because I don't know at which point to join members table to the addclique table.
$sql = "SELECT i.*, m.* FROM addclique i JOIN members m ON m.id = ******** WHERE adder_id = :id OR clique_id = :id";
$result= $db->query($sql, array('id' => $_SESSION['id']);
The query below will select all members that a particular person has added plus all members that have added the same person.
select m.* from members m
join addclique a on m.id = a.adder_id
where a.clique_id = :id
union
select m.* from members m
join addclique a on m.id = a.clique_id
where a.adder_id = :id
If I'm understanding your question correctly, you need to join on m.id = a.adder_id or m.id = a.clique_id. You can do this with in:
set #id := 4;
select *
from members m
join addclique a on m.id in (a.adder_id, a.clique_id)
where #id in (a.adder_id, a.clique_id)
and m.id != #id
SQL Fiddle Demo

Categories