mysql Selecting from two different tables. - php

$sql = "select body, stamp from posts where user_id = '$userid' order by stamp desc";
NOTE: the above query works fine. What I want to do is also select username from my users table, and display the username that matches the user_id.
I have edited the above statement like so, and it doesnt work. Can someone suggest the correct query? My goal is to also be able to display usernames. in addition to simply displaying user_id.
$sql = "select body, stamp from posts AND username from users where user_id = '$userid' order by stamp desc";
My goal is to also be able to display usernames. instead of simply user_id.

You'll need to use a JOIN to bring the two tables together on the matching field, so something like:
$sql = "SELECT p.body, p.stamp, u.username FROM posts p INNER JOIN users u ON p.user_id=u.user_id WHERE p.user_id='$userid' ORDER BY p.stamp DESC";

You can use table name to select the columns. Ex:
$Query = "select table1.body, table1.stamp, users.username from posts, users where user_id = '$userid' order by stamp desc";
But, this method is not good in performance.
The best method is:
$Query = "SELECT table1.body, table1.stamp, users.username
FROM posts
INNER/LEFT/RIGHT JOIN users
ON users.user_id = '$userid' AND users.user_stamp = stamp.stamp_id
All the tables must be related.
Greetings,

$sql = "select body, stamp from posts , username from users where user_id = '$userid' order by stamp desc";
I just corrected the syntactical error in your query... that is the AND keyword should be replaced by ,.

$sql = "
select body, stamp from posts where user_id = '$userid' order by stamp desc
UNION ALL
select body, stamp from username where user_id = '$userid' order by stamp desc
";
http://dev.mysql.com/doc/refman/5.0/en/union.html

You should use aliases or table name to avoid the duplicate problem like this
tablename.column
or
alias.column
you can set the alias in your where clause :
FROM table as alias_name

$sql = "SELECT p.body,p.stamp,u.username from posts p LEFT JOIN users as u ON (p.user_id = u.user_id) WHERE user_id = '$user_id' ORDER BY p.stamp DESC";
should do it

Try this:
select body, stamp, username
from posts p JOIN users a ON p.user_id = a.user_id
WHERE p.user_id = '$userid' order by stamp desc

This should work fine:
SELECT posts.body, posts.stamp, users.username
FROM posts, users
WHERE posts.user_id = '$userid' AND posts.user_id = users.user_id
ORDER BY posts.stamp DESC

select body, stamp, username
from posts,users
where users.user_id = post.user_id
and users.user_id = '$userid'
order by stamp desc;

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`;

Order a table by another table column?

I have this query:
$q = "SELECT * FROM user
WHERE sec='1' AND reg_by='".$_SESSION['login_username']."'
ORDER BY date DESC LIMIT $startrow, 30 ";
I have another table which stores appointments, it has a column named meet.
How can I sort this query by meet?
Not all data at users are in other table.
You can use the below query. Replace another_table with your original table name :
$q = "SELECT u.* FROM user AS u LEFT JOIN another_table AS at ON u.userid = at.userid WHERE u.sec='1' AND u.reg_by='".$_SESSION['login_username']."' ORDER BY at.meet DESC LIMIT $startrow, 30 ";
you can use joining for this like
select user.*,meet.* from user left join meet on (meet.userid = user.id) where user.sec='1' AND user.reg_by='".$_SESSION['login_username']."' order by meet.userid DESC
$q = "SELECT * FROM user INNER JOIN user
ON meets.userid=user.userid WHERE sec='1' AND reg_by='".$_SESSION['login_username']."' ORDER BY date DESC LIMIT $startrow, 30 ";

Order by rows of different table

I have "user_likes" table with id(int(10) key), user_id(varchar(15)), post_id(varchar(15)) and "user_post" table with post_id(int(15) key), user_id(varchar(15)), post_txt(text)
I now order the posts by id:
$que_posts = mysql_query("select * from user_post order by user_id DESC");
I want to order it by amount of likes to each post(i.e. posts with more likes will be first).
The problem is that the LIKES are in different table. How can I do it?
Edit- I using sef4eg's answer(with changes) I could fix it:
"SELECT user_post.*, COUNT(user_likes.post_id) AS like_count
FROM user_post LEFT JOIN user_likes
ON user_post.post_id = user_likes.post_id
GROUP BY user_post.post_id
ORDER BY like_count desc;";
Now you have like_count in your select so you display it if you need to
SELECT user_post.*, COUNT(user_likes.id) as like_count
FROM user_post
LEFT JOIN user_likes
ON user_likes.user_id = user_post.user_id AND user_likes.post_id = user_post.post_id
GROUP BY user_post.post_id
ORDER BY like_count DESC

Search query involving two tables in mysql

I have two tables, users and sales. sales.userId = user.id
I am trying to use following query. To search parameters in both users and sales.
SELECT * FROM sales INNER JOIN users ON users.id = sales.userId WHERE 'users.fullname' like '%Cloud%' OR 'users.storename' like '%cloud%' order by sales.id DESC
No result is showing up. What could be wrong in query.
e.g one user['fullname'] is CloudBuck Abc
if(isset($_GET["q"]) && $_GET["q"]!="")
{
$q = trim($_GET["q"]);
$where = "WHERE users.fullname like '%".$q."%' OR users.storename like '%".$q."%'";
}
$query = "SELECT *
FROM sales
INNER JOIN users ON users.id = sales.userId ".$where." order by sales.id DESC";
You are quoting the field names and this is not allowed, either use backticks if its a reserved word or do not use it.
SELECT * FROM sales
INNER JOIN users ON users.id = sales.userId
WHERE `users`.`fullname` like '%Cloud%'
OR `users`.`storename` like '%cloud%'
order by sales.id DESC
SELECT * FROM sales INNER JOIN users ON users.id = sales.userId
WHERE (users.fullname like '%Cloud%' OR users.storename like '%cloud%')
order by sales.id DESC
Remove the single qoutes around the column names:
SELECT *
FROM sales
INNER JOIN users ON users.id = sales.userId
WHERE users.fullname like '%Cloud%' OR users.storename like '%cloud%' order by sales.id DESC
Because mysql will use it as string if the single quote is set.

fields with the same name in different tables. how to get a specific one when fetching

while ($row = mysql_fetch_array($results)){
$row['fieldName']
}
$query = "SELECT SQL_CALC_FOUND_ROWS *
FROM realestate
INNER JOIN users ON id_user = users.id
LEFT JOIN pic ON id_realestate = realestate.id
{$place}
{$offer}
{$type}
{$price}
group by realestate.id
ORDER BY {$order}
LIMIT 0,10";
i have two tables users and realestate. both have the field id. what should be in the fileldname above to get id in users table?
You should name the fields in the query: if you do users.id AS user_id, then PHP will see a user_id field.
Of course, in this particular case it doesn't matter, since you have two equivalent fields: id_user and id_realestate.

Categories