Sort multiple Mysql queries by common field - mysql

I'm new at this and don't comprehend Mysql very well. I'm trying to retrieve ALL media uploaded by a certain user, and sort it all by the date it was uploaded. Problem is, the media is in 3 different tables. Can someone provide me with a good solution?
$sql1 = "SELECT * FROM videos WHERE user_id = $user_id";
$sql2 = "SELECT * FROM photos WHERE user_id = $user_id";
$sql3 = "SELECT * FROM audios WHERE user_id = $user_id";
SORT DESC BY $result['upload_date'];

You want to create a JOIN. If you only want rows where there are existing relationships, you can use an inner join, set up like this:
SELECT *
FROM videos v
JOIN photos p ON p.user_id = v.user_id
JOIN audios a ON a.useR_id = p.user_id
ORDER BY upload_date DESC;
The above will select all columns, which may have some repeated things. For example I believe user_id will show up once for each table you joined, so I would narrow down your select clause.

Related

Sql join two tables with another that has no common column

I have this subquery where I am getting the posts of users that a user is following. This is the subquery.
$query = "SELECT * FROM `Posts` WHERE UserID IN
(
SELECT Followed FROM `Follow` WHERE Follower = ?
)
ORDER BY PostDate DESC
";
// on bind_param ? will be $userID
This works fine but I also want to get the user's own posts and then data from a profiles table so I'm probably going to ditch the subquery for some sort of join. I've used inner joins before however the profile/posts table have a common id 'UserID' but the Follow table does not. Would a full join work or would I have to use an AS ?
A union might be best.
SELECT * FROM posts WHERE UserID = ?
UNION ALL
SELECT P.*
FROM posts P
INNER JOIN follow F ON F.followed = P.UserID
WHERE F.follower = ?

how to write this sql query with WHERE clause properly?

what is the right way to write this sql query
select * from articles where id = 1;
select * from users where id = user_id in articles table;
my question is how to write the second sql statement properly
From your question I am unable to understand what you are really looking for. I think you need to inner join two tables. The query below will give you the result by joining both the tables and it will consider USER_ID column in ARTICLES table represents ID column in USERS.
SELECT * FROM USERS INNER JOIN ARTICLES ON USERS.ID = ARTICLES.USER_ID WHERE USERS.ID = 1;
select * from users where user_id in (select id from articles);
With additional filtering inside select from articles for example. Again, depends on the requested result.
SELECT *
FROM articles
INNER JOIN users ON articles.id = users.user_id
WHERE id = '1'
Or use it as a sub query
SELECT *
FROM users
WHERE user_id IN (SELECT ID
FROM articles
WHERE id = '1')

PHP / MySQL Query merge 2 queries

I am wondering if there is an easier way to execute this logic in 1 query rather than using 2 queries and having to loop the second query within the first one.
Firstly I grab the all the users which have been referred by the current user_id.
SELECT user_id FROM users WHERE referral = $user_id
Then, I want to count the number of page hits (or whatever else I'm counting), for each of the user's referrals.
SELECT COUNT(hits) FROM tracking WHERE user = $user_id
Is there an easier way of doing this with a join?
Try with a left join and group by:
select u.user_id, count(t.hits) as hits
from users u left join tracking t on u.user_id = t.user
where u.referral = $user_id
group by u.user_id;

MySQL combing results

I have a table users and I have a simple search query:
SELECT *
FROM users
WHERE username LIKE 'rya%'
LIMIT 10
I also have another table called follows which basically is a list of users being followed by other users. I modified the above query to search only within what particular user is following:
SELECT users.*
FROM follows
INNER JOIN users ON users.id = follows.following_id
WHERE username LIKE 'rya%' AND follows.follower_id = 18
LIMIT 10
Most of the times, the query above only returns couple of results (because I am not search the entire users table).
What I want to do is combine the above 2 queries, to return a MAXIMUM of 10 results of usernames matching a certain string with first listing the user's following, and then search the entire table of users.
I already have a solution but it requires doing this at application level by first running the 2nd query, then first, and coming the two. Is there a way I can do all of this in 1 query?
Thanks
One way to do this is to search the entire user table, but order the results so that followers show up first. It's a little bit hard to understand what you want without schema, sample data, and output, but something like
SELECT users.*
FROM users
LEFT JOIN follows ON users.id = follows.following_id
WHERE username LIKE 'rya%'
ORDER BY follows.follower_id = 18 DESC
LIMIT 10
Notice that I used a LEFT JOIN in order to get all of the users. A different way would be to UNION the results of the two tables and wrap in a SELECT. I don't know why you would do it this way given the other option, but it would be something like
SELECT *
FROM (
SELECT users.*
FROM follows
INNER JOIN users ON users.id = follows.following_id
WHERE username LIKE 'rya%' AND follows.follower_id = 18
LIMIT 10
UNION
SELECT *
FROM users
WHERE username LIKE 'rya%'
LIMIT 10
) AS u
LIMIT 10
edit
Since the second one is working for you, I thought I throw a variation up using UNION ALL, which can be faster than UNION because it does not remove duplicate rows. You would have to make sure there were no duplicates yourself, which we can do like this
SELECT *
FROM (
SELECT users.*
FROM follows
INNER JOIN users ON users.id = follows.following_id
WHERE username LIKE 'rya%' AND follows.follower_id = 18
LIMIT 10
UNION ALL
SELECT users.*
FROM follows
INNER JOIN users ON users.id = follows.following_id
WHERE username LIKE 'rya%' AND follows.follower_id != 18
LIMIT 10
) AS u
LIMIT 10

MySQL: 3 table join query?

I have three tables (user, friends, posts) and two users (user1 and user2).
When user1 adds user2 as friend then user1 can see the posts of user2 just like on Facebook. But only the posts after the date when user1 added user2 as friend. My query is like this:
$sql = mysql_query("SELECT * FROM posts p JOIN friends f ON
p.currentuserid = f.friendid AND p.time >= f.friend_since OR
p.currentuserid='user1id' WHERE f.myid='user1id'
ORDER BY p.postid DESC LIMIT 20");
it is working all the way fine but with a little problem.....!!
it displays user2, user3 (all the users as friends of user1) posts for single time but shows user1 posts multiple.......i.e
user2. hi
user1. userssfsfsfsfsdf
user1. userssfsfsfsfsdf
user3. dddddddd
user1. sdfsdsdfsdsfsf
user1. sdfsdsdfsdsfsf
but i in database it is single entry/post why it is happening........!!
How can I fix it?
I'm not a SQL expert, but I think your problem is in the JOIN condition. I cannot see a way how you can join with posts and friends and get the result that you need. A SQL expert may know this, but for me it's just too difficult.
If I were you I would break the problem down in 2 parts:
Select the user's own posts
Select the user's friend's posts
For example, you can do this by using 2 different conditions and do the join with the friends table in a sub query (I have not tested this!):
select *
from posts p
where
p.currentuserid = 'user1id'
or
p.postid in
(
select p2.postid
from posts p2
join friend f on p2.currentuserid = f.friendid
where p2.time >= f.friend_since and f.myid='user1id'
)
Another way to do it is to use a union (also not tested..):
select *
from posts p
where
p.currentuserid = 'user1id'
union
select p2.*
from posts p2
join friend f on p2.currentuserid = f.friendid
where p2.time >= f.friend_since and f.myid='user1id'
I think, the easiest solution is to use GROUP BY statement on column posts.userId to remove duplicate entries. However it is not optimized way to solve the problem.
The reason you're getting the posts of all of user1's friends is you're not qualifying which friend's posts the query should return.
Add a f.friendid = 'user2id' (or whatever the column name is) in there before the WHERE clause.
You really should give some idea of what the schema looks like so we don't have to make so many assumptions. I'm assuming the primary key of user is id, and friends has a userid as well as a friendid field. I'm also assuming posts.currentuserid is the id of the user who created the post. If not, replace it with posts.userid or whatever the correct field is.
The reason your query doesn't work right is that you need at least 2 joins. When creating a query, it's easiest to start with what you have and work up to what you want, one join at a time. Here's the query to get the posts that a particular user can read:
SELECT p.*
FROM user u
JOIN friends f ON u.id = f.userid
JOIN posts p ON ((u.id = p.currentuserid) OR (f.friendid = p.currentuserid AND p.time >= f.friend_since))
WHERE u.id = ?
ORDER BY p.postid DESC LIMIT 20
The second join is where the meat is. It specifies that in order to read a post it (a) has to be written by you or (b) has to be written by a friend of yours AFTER you friended them.
If you want to also get the name of the user who created the post (assuming user.name holds the user name) you need a 3rd join:
SELECT pu.name as 'Posted By', p.*
FROM user u
JOIN friends f ON u.id = f.userid
JOIN posts p ON ((u.id = p.currentuserid) OR (f.friendid = p.currentuserid AND p.time >= f.friend_since))
JOIN user pu ON p.currentuserid = pu.id
WHERE u.id = ?
ORDER BY p.postid DESC LIMIT 20