MySql selecting last 10 rows without disturbing the order - mysql

I want to select the last 10 rows but the order should remain the asc only.
Its like displaying the last page in a forum but the post are still aligned in the right order.
I tried to do this
SELECT users.display,appreply.*,users.userid,users.avtar FROM appreply
LEFT JOIN users ON users.userid=appreply.userid
WHERE appreply.appid='$appid'
ORDER by apprepid DESC
LIMIT 10
But as i said this disturbs the order.
Please help me

try
SELECT X.* FROM
(
SELECT users.display,a.apprepid apprepid_ar, a.appid appid_ar, a.reply reply_ar, a.userid userid_ar, a.browser browser_ar, a.os os_ar, a.time time_ar,users.userid,users.avtar FROM appreply a
LEFT JOIN users ON users.userid=a.userid
WHERE a.appid='$appid'
ORDER by a.apprepid DESC
LIMIT 10
) X ORDER BY X.apprepid_ar ASC

You might be having an auto-increment field in the table, which is usually 'Id'.
Instead of using the DESC operator on the apprepid field, use the Id field as:
SELECT users.display,appreply.*,users.userid,users.avtar FROM appreply
LEFT JOIN users ON users.userid=appreply.userid
WHERE appreply.appid='$appid'
**ORDER by id DESC**
LIMIT 10 ORDER BY apprepid ASC

Related

Searching and Sorting Using MySQL Inner Join

I guess I can't explain my problem properly. I want to explain this to you with a picture.
Picture 1
In the first picture you can see the hashtags in the trend section. These hashtags are searched for the highest total and it is checked whether the date has passed. If valid data is available, the first 5 hashtags are taken.
Picture 2
In the second picture, it is checked whether the posts in the hashtag are in the post, if any, the oldest date value is taken, LIMIT is set to 1 and the id value from the oyuncular table is matched with sid. Thus, the name of the person sharing can be accessed.
Picture 3
My English is a little bad, I hope I could explain it properly.
SELECT
social_trend.hashtag,
social_trend.total,
social_trend.tarih,
social_post.sid,
social_post.tarih,
social_post.post,
oyuncular.id,
oyuncular.isim
FROM
social_trend
INNER JOIN
social_post
ON
social_post.post LIKE '%social_trend.hashtag%' ORDER BY social_post.tarih LIMIT 1
INNER JOIN
oyuncular
ON
oyuncular.id = social_post.sid
WHERE
social_trend.tarih > UNIX_TIMESTAMP() ORDER BY social_trend.total DESC LIMIT 5
YOu should use a sibquery
and add a proper join between subqiery and social_trend
(i assumed sing both sid)
SELECT
social_trend.hashtag,
social_trend.total,
social_trend.tarih,
t.sid,
t.tarih,
t.post,
oyuncular.id,
oyuncular.isim
FROM (
select social_post.*
from social_post
INNER JOIN social_trend ON social_post.post LIKE concat('%',social_trend.hashtag,'%' )
ORDER BY social_post.tarih LIMIT 1
) t
INNER JOIN social_trend ON social_trend.hashtag= t.post
INNER JOIN oyuncular ON oyuncular.id = t.sid
WHERE
social_trend.tarih > UNIX_TIMESTAMP() ORDER BY social_trend.total DESC LIMIT 5
but looking to your new explanation and img seems you need
SELECT
t.hashtag,
t.total,
t.tarih_trend,
t.sid,
t.tarih,
t.post,
oyuncular.id,
oyuncular.isim
FROM (
select social_post.sid
, social_post.tarih
, social_post.post
, st.hashtag
, st.total
, st.tarih tarih_trend
from social_post
INNER JOIN (
select * from social_trend
WHERE social_trend.tarih > UNIX_TIMESTAMP()
order by total DESC LIMIT 5
) st ON social_post.post LIKE concat('%',st.hashtag,'%' )
ORDER BY social_post.tarih LIMIT 5
) t
INNER JOIN oyuncular ON oyuncular.id = t.sid

Multiple Order By in MySql Query

I am developing a voting system.
I am facing issue in ordering.
Basically i want to get top ranking and maximum nominee first. I used "totalUserVoted" and "totalRating" in Descending order on both condition but my query ordering "totalUserVoted".
I am expecting the result in this order.
Here is my sql query.
SELECT
(SELECT (((SUM(`design`)*4)+(SUM(`usability`)*3)+(SUM(`creativity`)*2)+(SUM(`content`))*1))/ count(`nominee_id`) / 10
FROM `sk_award_nominee_rating`
WHERE `sk_award_nominee_rating`.`nominee_id`=`sk_award_nominee`.`nominee_id`) AS totalRating,
(SELECT count(`nominee_id`)
FROM `sk_award_nominee_rating`
WHERE `sk_award_nominee_rating`.`nominee_id`=`sk_award_nominee`.`nominee_id`) AS totalUserVoted,
`sk_award_nominee`.*,
`sk_user`.`username`,
`sk_user`.`email`,
`sk_user_profile`.`f_name`,
`sk_user_profile`.`m_name`,
`sk_user_profile`.`l_name`,
`sk_user_profile`.`address`
FROM `sk_award_nominee`
LEFT JOIN `sk_user` ON `sk_user`.`user_id`=`sk_award_nominee`.`user_id`
LEFT JOIN `sk_user_profile` ON `sk_award_nominee`.`user_id`=`sk_user_profile`.`user_id`
WHERE `sk_award_nominee`.`status` = 1
AND DATE(approval_date) = '2016-02-22'
ORDER BY `totalUserVoted` DESC,
`totalRating` DESC
Something like this ?
ORDER BY totalUserVoted DESC, totalRating DESC
PHP MySQL Order by Two Columns

I am trying this QUERY, and return this weird error,please help me

select tbl_c_food_veg.pk_veg_id,tbl_c_food_veg.var_desc,tbl_c_food_veg.var_title ,tbl_c_food_veg_img.var_img,( select tbl_c_food_non_veg.pk_non_veg_id,tbl_c_food_non_veg.var_desc,tbl_c_food_non_veg.var_title,tbl_c_food_non_veg_img.var_img from tbl_c_food_non_veg left join tbl_c_food_non_veg_img on tbl_c_food_non_veg.pk_non_veg_id=tbl_c_food_non_veg_img.fk_non_veg_id where
tbl_c_food_non_veg.fk_cat_id=8 and tbl_c_food_non_veg.pk_non_veg_id!=0 group by tbl_c_food_non_veg_img.fk_non_veg_id order by tbl_c_food_non_veg.pk_non_veg_id desc limit 2 ) as non_veg,( select tbl_c_food_drinks.pk_drinks_id, tbl_c_food_drinks.var_desc, tbl_c_food_drinks.var_title, tbl_c_food_drinks_img.var_img from tbl_c_food_drinks left join tbl_c_food_drinks_img on tbl_c_food_drinks.pk_drinks_id=tbl_c_food_drinks_img .fk_drinks_id where
tbl_c_food_drinks.fk_cat_id=8 and tbl_c_food_drinks.pk_drinks_id!=0 group by tbl_c_food_drinks_img.fk_drinks_id order by tbl_c_food_drinks.pk_drinks_id desc limit 2 ) as drinks from tbl_c_food_veg left join tbl_c_food_veg_img on tbl_c_food_veg.pk_veg_id=tbl_c_food_veg_img.fk_veg_id where
tbl_c_food_veg.fk_cat_id=8 and tbl_c_food_veg.pk_veg_id!=0 group by tbl_c_food_veg_img.fk_veg_id order by tbl_c_food_veg.pk_veg_id desc limit 2
In your nested sub queries for retrieving non_veg and drinks you use limit 2 so most likely the subquery will return 2 rows, but you can only use one row. Change the limit to limit 1
and try using aliases as Barranka says it wil be much easier to read your query

Limit Within A Subquery

I have a messages table that can contain the primary message, as well as sub-messages. What i want to achieve is only get the top 2 most recent sub-messages.
SELECT message.id, message.date_sent,message.object_id, message.content,
SubMessage1.content, SubMessage1.id, SubMessage1.date_sent,
SubMessage2.content, SubMessage2.id, SubMessage2.date_sent
FROM messages As Message
LEFT
OUTER
JOIN ( SELECT messages.object_id as object_id, messages.content as content,
messages.id as id , messages.date_sent as date_sent
FROM messages
ORDER
BY messages.date_sent ASC LIMIT 1,1
) as SubMessage1
ON Message.id = SubMessage1.object_id
LEFT
OUTER
JOIN ( SELECT messages.object_id as object_id, messages.content as content,
messages.id as id , messages.date_sent as date_sent
FROM messages
ORDER
BY messages.date_sent ASC LIMIT 2,1
) as SubMessage2
ON Message.id = SubMessage2.object_id
WHERE Message.id = 1
When I remove the limit, I always get the first result. But when I put in the limit I get null values. Any ideas? Suggestions?
SELECT * FROM messages WHERE id = 1
Union all
SELECT * FROM messages WHERE object_id=1
ORDER BY date_sent ASC LIMIT 2
The problem here is that the ORDER BY ... LIMIT ... in the subqueries is being applied before the ON Message.id = ... .object_id in the joins. As a result, the joins only have the chance to examine one message and determine if it's a submessage. Most of the time, it won't be, so you'll get NULL values.
To fix this, you need to move the content of your ON-clauses into WHERE-clauses inside the subquery. Normally that would be a bit of a mess, but since you're only looking to retrieve a single-record result-set, it's not too bad:
SELECT message.id, message.date_sent, message.object_id, message.content,
SubMessage1.content, SubMessage1.id, SubMessage1.date_sent,
SubMessage2.content, SubMessage2.id, SubMessage2.date_sent
FROM messages AS Message
LEFT
OUTER
JOIN ( SELECT messages.object_id AS object_id, messages.content AS content,
messages.id AS id, messages.date_sent AS date_sent
FROM messages
WHERE messages.object_id = 1
ORDER
BY messages.date_sent ASC LIMIT 1,1
) AS SubMessage1
ON TRUE
LEFT
OUTER
JOIN ( SELECT messages.object_id AS object_id, messages.content AS content,
messages.id AS id, messages.date_sent AS date_sent
FROM messages
WHERE messages.object_id = 1
ORDER
BY messages.date_sent ASC LIMIT 2,1
) AS SubMessage2
ON TRUE
WHERE Message.id = 1
;
(Note that this includes the parameter three times in the query: less than ideal, perhaps, but really not a problem.)
If you want 2 rows, use
LIMIT 2
In SQL land, counting is from 1 not 0.
Also, in mysql LIMIT with 2 params is:
LIMIT offset, row_count
But with 1 param it's:
LIMIT row_count
I'd like to meet the genius who decided to move the param like that and kick his in the pants.
The LIMIT command's starting index is 0. You may want to change your limits to (0, 1) and (1, 1).

mysql random least used

I need to select a record at random but not one already selected before unless all records have been selected.
Table Setup:
_word (id, nam)
_word_tm (id, word_id, tm)
Every time a word is used it is loaded into _word_tm. What I am wanting to do is make sure I use all the words before I reuse an already used word.
What I am really looking for is something like the below but just trying to figure out how to mesh.
select w.nam FROM _word w LEFT JOIN _word_tm wt ON w.id = wt.word_id ORDER BY count(wt.id) asc, rand() limit 1
First, find out how many times the least-used words have been used:
select _word.id, count(*) c from _word
left join _word_tm on _word.id=_word_tm.word_id order by c limit 1;
Store that value (from c) in a variable $least_used. Then get all the words used that many times, in random order:
select _word.id, count(*) c from _word
left join _word_tm on _word.id=_word_tm.word_id
group by _word.id having c <= {$least_used}
order by rand() limit 1;
You should be able to do something like this:
SELECT * FROM word_table WHERE word NOT IN (SELECT word FROM words_used) ORDER BY rand() LIMIT 1;
You will need to have an updated copy of MySQL for this to work. Also, you would need to include a line of code afterward or before hand to clear/reset your words_used table once it had the same contents as word_table.
You can try the following SQL to return a random row
select A.nam
from _word A
where A.id not in (select B.id from _word_tm B)
order by rand()
limit 1
if the above does not return any reocrd , do a simple
select A.nam
from _word A
order by rand()
limit 1