how to left join the relation to join five latest rows only - php

i am joining the latest relation row with the following code.
SELECT pos.* FROM users AS usr LEFT JOIN posts AS pos ON pos.user_id = usr.id WHERE
pos.id = (
SELECT MAX(id)
FROM posts
WHERE user_id = usr.id
);
if I want to join the second last row only here is the code
SELECT pos.* FROM users AS usr LEFT JOIN posts AS pos ON pos.user_id = usr.id WHERE
pos.id = (
SELECT id
FROM posts
WHERE user_id = usr.id
ORDER BY id DESC LIMIT 1,1
);
The question is what i can do if I want to join the latest five rows again in each relation?

Related

How to use SQL CASE for user id count up to 5 and display data in column.?

In word-press I am joining 3 tables users, user_meta and one custom table student.
Here in student custom table am having maximum 5 student and student data is storing according to user id. So i am trying to retrieve all student as per userid in a single query.
See what I have tried:
SELECT
u.id,
COUNT(u.id) as child_count,
u.display_name,
u.user_email AS login,
u.user_nicename AS u_name,
u.user_registered AS reg_on,
m1.meta_value AS parent_fname,
m2.meta_value AS parent_fname,
s.firstname as child_fname,
s.lastname as child_lname,
s.year_group as child_age,
s.subjects as child_int_sub
FROM wp_users u
JOIN wp_usermeta m1 ON (m1.user_id = u.id AND m1.meta_key = 'billing_first_name')
JOIN wp_usermeta m2 ON (m2.user_id = u.id AND m2.meta_key = 'billing_last_name')
INNER JOIN wp_usermeta m3 ON (m3.user_id = u.id AND m3.meta_key = 'wp_capabilities' AND m3.meta_value LIKE '%subscriber%')
JOIN wp_student_table s ON (s.parent_id = u.ID)
GROUP BY s.id;
and if I run this query in phpmyadmin
So how I can use case for
userid count = 1 then column(child1_name,child1_age )
userid count = 2 then column(child2_name,child1_age )
and so on..
to show all child of same user in single row.

Select a row from two tables depend on primary key(Mysql)?

I have two table users and album. In users table there is user_id primary key .In other table albums there are multiple rows with that user_id because every time when a user upload a new album it uploads with user_id as foreign key. I want to select only once the user_id with other table(album) ignore other result set.
How can I achieve this?
SELECT a.*, b.*
FROM users a
INNER JOIN album b
ON a.user_ID = b.user_ID
INNER JOIN
(
SELECT user_ID, MAX(photo_id) max_rec
FROM album
GROUP BY user_ID
) c ON b.user_ID = c.user_ID AND
b.photo_id = c.max_rec
SELECT album.* FROM album LEFT JOIN users ON user.id = album.id WHERE user.id = SOMEIDHERE
I believe this will work, your not giving me a whole lot of info to work with.
SELECT *
FROM ( SELECT u.*, a.*
FROM users AS u
INNER JOIN album AS a
ON u.user_ID = a.user_ID
ORDER BY a.created DESC) AS h
GROUP BY user_ID
ORDER BY b.created DESC -> ORDER BY whatever row you wish for. In this case the newest one is chosen.

Counting rows from table after joins

I have a query that selects data from 4 tables via joins, i want to also count the number rows in a fifth table containing a matching foreign key.
This is what my current query looks look like, and it doesnt work
"SELECT
ph.pheed_id,ph.user_id,ph.datetime,ph.repheeds,
ph.pheed,fav.id,fav.P_id,fav.datetime as stamp,
u.username,ava.avatar_small
COUNT(pheed_comments.comment_id) as comments
FROM favourite_pheeds fav
INNER JOIN pheeds ph ON ph.pheed_id=fav.P_id
INNER JOIN users u ON u.id=ph.user_id
INNER JOIN profiles pr ON pr.user_id=ph.user_id
LEFT JOIN user_avatars ava ON ava.avatar_id=pr.avatar
ORDER BY stamp DESC
LIMIT $offset,$limit";
How do i count the number rows in a fifth table containing a matching foreign key.
select ph.pheed_id,
ph.user_id,
ph.datetime,
ph.repheeds,
ph.pheed,
fav.id,
fav.P_id,
fav.datetime as stamp,
u.username,
ava.avatar_small,
coalesce(pcc.Count, 0) as comments_count
from favourite_pheeds fav
inner join pheeds ph on ph.pheed_id = fav.P_id
inner join users u on u.id = ph.user_id
inner join profiles pr on pr.user_id = ph.user_id
left join user_avatars ava on ava.avatar_id = pr.avatar
left outer join (
select pheed_id, count(*) as Count
from pheed_comments
group by pheed_id --took a guess at the column name here
) pcc on ph.pheed_id = pcc.pheed_id
order by stamp desc
LIMIT $offset, $limit

Multiple tables, multiple statement

Let's take 3 tables that has all tons of rows:
TABLE Posts
PostPID
PostUID
PostText
TABLE Users
UserUID
UserName
TABLE Favorites
FavoriteUID
FavoritePID
Now, in order to get all the recent posts I perform a query such as:
SELECT p.PostPID, p.PostUID, p.PostText, u.UserUID, u.UserName
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
ORDER BY p.PostPID DESC
LIMIT 0, 30
Which works fine. Now I was wondering, how could I get only the posts a certain UserUID prefers? So only the one with FavoriteUID = UserUID = X?
You could use a subquery.
...
Where p.PostUID in (select f.FavoritePID from Favorite f where f.FavoriteUID = UserUID)
...
second join will do the same
SELECT
p.PostPID, p.PostUID, p.PostText, u.UserUID, u.UserName
FROM
Posts AS p
JOIN
Users AS u ON p.PostUID = u.UserUID
Join
Favorites as f on f.FavoriteUID = u.UserUID and f.FavoritePID=p.PostPID
ORDER
BY p.PostPID DESC
LIMIT 0, 30

SQL/PHP: SELECT only one row per item

I have a table containing persons information (one row per person) and another table containing persons photos filenames (many rows per person). I want to select a group of persons (based on another table) but only one photo per person.
My old SQL was like this:
SELECT persons.personID, persons.name, persons.photo_filename, movie_cast.role
FROM persons, movie_cast
WHERE persons.personID = movie_cast.personID
AND movie_cast.imdbID = ?
ORDER BY movie_cast.castORDER
LIMIT 9';
But here, the 'persons' table contains also a 'photo_filename' column. In my new database design, this column is in another table. So if I try to get the photo_filename from the new table I get all the photos available for each person, but I need to get only one.
How to do it?
In the first example I have assumed there is always a photo, and am just grabbing the highest sorted photo filename alphabetically as a means to get a consistent photo for each user each time you run the query.
SELECT p.personID, p.name, ph.photo_filename, mc.role
FROM persons p
INNER JOIN movie_cast mc ON p.personID = mc.personID
INNER JOIN (
select personID, max(photo_filename) as MaxPhotoName
from photos
group by personID
) phm on p.personID = phm.personID
INNER JOIN photos ph on phm.personID = ph.personID
and phm.MaxPhotoName = ph.photo_filename
WHERE mc.imdbID = ?
ORDER BY mc.cast
LIMIT 9
If there is a photo_date column and you want to use the newest photo you can do it like this:
SELECT p.personID, p.name, ph.photo_filename, mc.role
FROM persons p
INNER JOIN movie_cast mc ON p.personID = mc.personID
INNER JOIN (
select personID, max(photo_date) as MaxPhotoDate
from photos
group by personID
) phm on p.personID = phm.personID
INNER JOIN photos ph on phm.personID = ph.personID
and phm.MaxPhotoDate = ph.photo_date
WHERE mc.imdbID = ?
ORDER BY mc.cast
LIMIT 9
If there is not always a photo, you can use a LEFT OUTER JOIN so that you will still get all your records back:
SELECT p.personID, p.name, ph.photo_filename, mc.role
FROM persons p
INNER JOIN movie_cast mc ON p.personID = mc.personID
LEFT OUTER JOIN (
select personID, max(photo_date) as MaxPhotoDate
from photos
group by personID
) phm on p.personID = phm.personID
LEFT OUTER JOIN photos ph on phm.personID = ph.personID
and phm.MaxPhotoDate = ph.photo_date
WHERE mc.imdbID = ?
ORDER BY mc.cast
LIMIT 9

Categories