I have a table that consists of the following fields
id, date, featured
featured is a bit which is 0 or 1
I want to order my MySQL table by if it's featured or not(featured = 1) first then I want the rest of the table to be ordered by the date.
I was only able to order it by one or the other.
This is my current SQL statement
SELECT * FROM listings ORDER BY featured = 1 DESC, date DESC
SELECT * FROM listing ORDER BY verified DESC, date DESC
Remove the = 1 from your query
It sounds like you want to select items which are verified (verified = 1) and then ordered by date (most recent first, which is DESC). You probably want:
SELECT * FROM listing WHERE verified = 1 ORDER BY date DESC
Related
When the data having same date( even the micro second ) like below table.
If I There is a table below
Create_At
User_ID
Balance
2022-09-29 09h:09:01.761335
4
200300
2022-09-30 12h:09:47.405520
6
58111
2022-09-30 12h:09:47.405520
6
53861
2022-09-29 11h:09:46.276274
6
79011
I would like to get the latest record per user ID.
When I try to sort the Create_At column by descending order as follow,
SELECT * FROM Balance_Table ORDER BY Create_AT Desc;
What is the logic behind this sorting?
Your current query is just sorting the entire table on the created at time. You could use the following LIMIT query to find the single most recent record:
SELECT * FROM Balance_Table ORDER BY Create_AT DESC LIMIT 1;
But, this would just return a single user's records. Instead, what you want to be using here on MySQL 8+ is ROW_NUMBER:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY User_ID ORDER BY Create_AT DESC) rn
FROM Balance_Table
)
SELECT Create_At, User_ID, Balance
FROM cte
WHERE rn = 1;
Given that some users have more than one record as tied for the latest, it might make more sense to use RANK(), instead of ROW_NUMBER(), and report all ties.
SELECT * FROM table ORDER BY score DESC
Above query ordering the data only by the score. If you want to order the data by another field as you mentioned, you have the add that field also to the query as below. You have mentions that you want to order by name in acceding order (C > D >E). So I used ORDER BY ASC. Below query will give you the output that you requested.
SELECT * FROM table ORDER BY score DESC, name ASC
I have a below db table structure :
Tablename : agency_mst
Fields:
id
name
is_premium
date_added
I want to sort the data so that the agencies with is_premium=1 comes first and the rest of the data is sorted by date_added desc order.
But also the is_premium=1 records should be sorted in random order. so first set of premium agencies will have random order. How can I do that using MySQL Select query.
I have built this query partially but not sure how to filter specific set of data. Below is that query:
SELECT * FROM agency_mst
ORDER BY is_premium DESC, date_added DESC
How about
SELECT *
FROM agency_mst
ORDER BY IF(is_premium=1, RAND(), -1.0), date_added DESC
That will use random ordering for the matching rows, then put the others last, and order them by date.
Be careful with performance, though. ORDER BY RAND() in any variant is a notorious performance antipattern in tables with many rows.
select t1.id,t1.name,t1.is_premium,t1.date_added from
(select (ROW_NUMBER() over (order by id))* cast(CRYPT_GEN_RANDOM(10) as int) RND,* from agency_mst) t1
order by t1.RND
SELECT id, name, is_premium , date_added ,if(is_premium ,rand()*-15,0) as first_order
FROM agency_mst
ORDER BY first_order, date_added DESC
Check This Using rand()*-15 you get -velue it show first and remain will be 0 and that will be order by date_added
Hi I am trying to select row in mysql whose visitor id=1 and max id then it should be the last row as I focus in this picture
but it is showing something else, it showing this output
here the mysql code I tried
SELECT *,max(id) FROM activity WHERE visitorid=1
you can do it with:
SELECT * FROM activity WHERE visitorid = 1 ORDER BY id DESC LIMIT 1
this will order your rows (where visitorid = 1) descending by id and select only the first one.
When there exists multiple entries for an id in a table
and you still want to fetch the latest, then
filter the records in descending order of auto incremented primary key and limit to top record.
Example:
select * from activity
where visitorid = 1
order by id desc
limit 1
I have a query that retrieves the reservation made by a team
the query computes and retrieves good but the problem is that I only want to retrieve the latest reservation made by the team but my query shows their first reservation made.
Here is the complete query
select
tbl_lab_reservations.id,
tbl_lab_reservations.full_desc,
serial_number,
rsvn_owner,
reservation_id,
reservation_date_end,
reservation_date_start,
(SELECT DATEDIFF( if(reservation_date_end = '0000-00-00', CURDATE(), reservation_date_end),
reservation_date_start)+1) as totalNumberOfDaysReserve
from tbl_lab_reservations
join tbl_lab_assets on tbl_lab_assets.id = tbl_lab_reservations.lab_id
where tbl_lab_reservations.full_desc = 'Dell Optiplex 380'
and tbl_lab_reservations.asset_status = 'Idle'
group by serial_number, rsvn_owner
ORDER BY tbl_lab_reservations.id ASC
The query that you have given is correct for showing the records from first to latest as the order by clause is asc. To retrieve the latest record,
change the order by clause to desc
from which you will get the latest record as the first one in the result(only if you have the tbl_lab_reservations.id is unique for all records).
To get only the latest record and omit the other, you have the limit keyword. The limit should be used at the end of the query to set the limit of records to be fetched.
Syntax : limit N ,where N specifies the number of record.
Example for you.
link
select id, events from msql
where id < '05'
group by id
order by id desc
limit 1
I am using this query but it is not working in mysql
SELECT TOP 1 * FROM (SELECT TOP 5 * FROM ads ORDER BY id DESC) ads ORDER BY id DESC
You can use ORDER BY ... LIMIT {[offset,] row_count | row_count OFFSET offset} (lets say you want to get 5 records from 10th - 10,11,12,13,14):
SELECT * FROM ads
ORDER BY id DESC
LIMIT 10,5
Although I assume you don't want to get them sorted by id but rather by views or similar criteria where ORDER BY views DESC would take a place (don't forget to to add index on views count).
The 'TOP' does not function on MySQL. You can edit the query in the following manner to get the job done
SELECT * FROM (SELECT * FROM ads ORDER BY id DESC LIMIT 5) ads ORDER BY id DESC LIMIT 5