Php and MysQL JOIN tree table - php

I have one question about JOIN from PHP section .
I know this question have in stackoverflow.com but that solution is not helped me.
I am using this code for join two table:
<?php
$select_posts = "SELECT users.uid,users.user_name,user_uploads.uid_fk,user_uploads.image_path
FROM users
JOIN user_uploads
ON users.uid = user_uploads.uid_fk
WHERE user_name='$user_name' order by rand() LIMIT 0,4";
$run_posts = mysql_query($select_posts);
while($row=mysql_fetch_array($run_posts)) {
$image_path=$row['image_path'];
$uid_fk = $row['uid_fk'];
?>
This code will work but i want to add another table here.
other table name is: message
table inside have:
msg_id
uid_fk
comment_count
like_count
How can I add a third table here? Anyone can help me!

Just join the third table with an another JOIN keyword in your query.
SELECT table_users.uid,table_users.user_name,table_user_uploads.uid_fk,table_user_uploads.image_path, table_message.msg_id
FROM table_users
JOIN table_user_uploads
ON table_users.uid = table_user_uploads.uid_fk
JOIN table_message
ON table_message.uid_fk = table_users.uid
WHERE user_name='$user_name' order by rand() LIMIT 0,4

Related

Get the last records from 3 tables

My tables
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY id DESC
LIMIT 1";
This is my SQL query, my task is to show the last records from 3 tables, but the table is blank, I don't know why,thanks in advance people :)
I guess the problem is coming from the ORDER BY id DESC .
Indeed, you have no column so called id.
You should probably remove this clause, in order to make your code work.
If you want to take the last records anyway, you can put an ORDER BY address_id DESC which will do the job !
The code directly edited :
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY adress_id DESC
LIMIT 1";
This may work:
SELECT a.address_id, u.user_id, n.note_id
FROM addresses a
LEFT JOIN users_addresses ua ON ua.ua_address_id = a.address_id
LEFT JOIN users u ON u.user_id = ua.ua_user_id
LEFT JOIN notes n ON n.note_user_id = u.user_id
ORDER BY a.address_id DESC
LIMIT 1
Here is the query to get all data from all the tables, not sure what do you mean last records from 3 tables, I can see four tables there:
SELECT *
FROM `addresses`
LEFT JOIN `users_addresses` ON `users_addresses`.`ua_address_id` = `addresses`.`address_id`
LEFT JOIN `users` ON `users`.`user_id` = `users_addresses`.`ua_user_id`
LEFT JOIN `notes` ON `notes`.`note_user_id` = `users`.`user_id`;

how to join with tables?

First I want to write a join query to get every doc and the subjects that he uploaded i used this query but it is not working well and showing all the subjects under one doctor and all i have to get the subjects of the course is the course ID
SELECT users.ID
, fullname
, Sub_ID
, Sub_name
, Sub_ext
, Sub_path
, subject.created_at
FROM users
JOIN subject
ON users.ID = subject.ID
WHERE C_ID = '$C_ID'
Your query is wrong...
Correct is...
"SELECT u.ID,u.fullname,s.Sub_id,s.Sub_name,s.Sub_ext,s.Sub_path,s.Created_at
FROM users u LEFT JOIN subject s
ON u.ID=s.ID
WHERE s.C_ID='.$C_ID.'";
i solved it by using 2 queries
first one to get doc in that course and the second one from the subjects that doctor give
$query = "SELECT `doc-course`.ID,fullname
FROM `doc-course`
JOIN users ON `users`.`ID` = `doc-course`.`ID`
WHERE C_ID='$C_ID' ";
the second
$query2 = "SELECT Sub_ID,Sub_name,Sub_ext,Sub_path,`subject`.`created_at`
FROM subject
WHERE `subject`.`ID` = '$ID[$x]'
";

INNER JOIN is not working

I have two tables: questions, and tracking.
questions holds the columns id and url.
tracking holds the columns q_id and user_id.
What I want to do is to select a random url for all rows, except where q_id = id and where user_id is equal to the $_SESSION["id"] ($id).
Here's my query:
$results = $db->query("SELECT url FROM questions
INNER JOIN tracking ON
questions.id != tracking.q_id
WHERE tracking.user_id = '$id'
LIMIT 1 ORDER BY RAND()");
$rows = $results->fetch_array();
The query I am doing is not working for some reason, what am I doing wrong?
Following is the query which I worked for me in SQL.
SELECT top 1 url FROM Questions
INNER JOIN Tracking ON
Questions.id != Tracking.qId
WHERE tracking.UserId = 1
ORDER BY NEWID()
For MySQL, you can try "ORDER BY uuid()"
That should resolve your issue.
Thanks and regards,
Chetan Ranpariya

using LIMIT and ORDER BY with LEFT JOIN in sql query

I'm a bit new to JOIN and I'm finding it difficult to understand how I can query one table with ORDER BY and LIMIT and using only ORDER BY on my JOINED 'right' table I think it is? So Basically if I was to query the two tables individually I would use these queries:
SELECT * FROM posts ORDER BY dateSubmitted DESC LIMIT ?,5
'?' standing for my bind_param() because I'm creating a pagination. Now for my 'right' Second table:
SELECT * FROM postcomments WHERE postcomments.postID = posts.ID ORDER BY dateSubmitted DESC
As far as my understanding goes to 'link' the two tables together I want to be using LEFT JOIN so that I will receive all my data from the 'left' table (being posts).
SELECT * FROM posts LEFT JOIN postcomments ON postcomments.postID = posts.ID
Now I can do this but I'm unsure where I would but my ORDER BY and LIMIT for both tables?
I've seen several different ways and I think this is what's getting me confused like I've seen this:
SELECT p.* FROM posts p ORDER BY posts.dateSubmitted DESC LIMIT ?,5
LEFT JOIN (SELECT * FROM postcomments
WHERE postscomments.postID = p.ID ORDER BY postcomments.dateSubmitted);
But I'm really unsure how to structure my query :S Any help appreciated :)
It will be at the end like this:
Select * from
(SELECT * FROM posts ORDER BY dateSubmitted DESC LIMIT ?,5) as tempPost
LEFT JOIN postcomments on (postscomments.postID = tempPost.ID)

expanding a mysql JOIN query in PHP

I need to alter my existing JOIN query below to also include the data from users.image correlating to the UserID of the post maker. Something like:
users.image WHERE users.UserID = posts.userid
I am not very good with join queries yet. How would I do this?
Existing Query:
$result = mysql_query("SELECT posts.* FROM listen JOIN posts ON posts.userid = listen.listenid WHERE listen.userid = '$user_id' ORDER BY DATE desc") or die(mysql_error());
Just add another JOIN clause:
SELECT posts.*
FROM listen
JOIN posts ON (posts.userid = listen.listenid)
JOIN users ON (users.UserID = posts.userid)
WHERE listen.userid = '$user_id'
ORDER BY DATE desc
You may need to change the JOIN to a specific join such as LEFT JOIN, depending on what you're after.
Btw, it is easier to see the query on multiple lines.
Edit: You'll probably want to add additional items that you are selecting with your fields, such as SELECT posts.*, users.*

Categories