Order by date and field - mysql

I have SELECT query with LEFT JOINT (SELECT) and order by COALESCE(SELECT2.date, SELECT1.date) but i need to order it too with important field.
So: if row have important = 1 it need to be first, then important = 0 but ordered by coleasce date so as so. Is this even posible?

You could place the important column first in the order by list. Sorting desc (for descending) will place 1 before 0:
order by
important DESC
, coalesce(select2.date, select1.date)
You can even use case to sort the important column in a custom way:
order by
case
when important = 'SENATOR' then 1
when important = 'PATRICIAN' then 2
when important = 'PLEBS' then 3
else 4
end
, coalesce(select2.date, select1.date)

Related

How to first get a specific row by id and after order the rest alphabetically by specific column

I am trying to get a specific id (53) to be first in order when retrieving my data and after that order title alphabetically. I found some answers here but none were what I am looking for.
Currently this is my query:
SELECT *
FROM snm_categories
WHERE parent_id = ".$conn->real_escape_string($getcat['id'])."
AND published = 1
ORDER BY CASE WHEN id = 53 THEN 0 ELSE 1 END, id, title
This correctly shows the first result with id 53 but the remaining items are not ordered alphabetically by title. How can I do that?
ORDER BY CASE WHEN id = 53 THEN 0 ELSE 1 END, title, id,
You have to change the sequence of the fields. You write the title column first before the id column. It matters.
MySQL treats boolean values as numbers, with "1" for true and "0" for false.
So, you can simplify the logic to:
ORDER BY (id = 53) DESC, title, id

MySQL ORDER BY CASE + operator

I am trying to do a search that would be sorted by relevance.
Let's say the search term contains 3 words: A, B and C. What I am trying to do is to check if the search term is present in the SELECT result and if yes that would increase its rank.
ORDER BY CASE
(
WHEN search_word_A_is_present THEN +1
WHEN search_word_B_is_present THEN +1
WHEN search_word_C_is_present THEN +1
ELSE 0
END
)
DESC
While there is no syntax error and the search runs and sorts by something (that seems different from what I want) but I am not sure what is being added up if anything. How would I go about seeing what the final rank (sum) is at the end for each result? Is this the correct way to do it?
Since in MySQL boolean conditions result in 1 and 0, you can simply add those up
ORDER BY search_word_A_is_present + search_word_B_is_present + search_word_C_is_present
DESC
A more practical example:
ORDER BY col1 = 1 + col2 = 'A' + col3 = 44 DESC

Mysql limit when the whole list may change

I have a query that returns 10 items of a big list. For example:
SELECT * FROM users
WHERE user_is_block = 0
AND user_is_paid = 1
ORDER BY user_post_hour ASC, id ASC"
When I put limit ?, ? to get a part of this list each time, a problem happens. Imagine we get the first 10. The row with id 11, is the first one in the next request. But as we use ORDER BY user_post_hour, the id 11 may go up and become 10. So when second request is sent, we never see id 11.
Any idea?
It might be the problem with datatype soring, again cast your column value to an integer explicitly
SELECT * FROM users
WHERE user_is_block = 0
AND user_is_paid = 1
ORDER BY cast(user_post_hour as unsigned) ASC, id ASC
Use this trick.
SELECT * FROM users
WHERE user_is_block = 0
AND user_is_paid = 1
ORDER BY user_post_hour+0 ASC, id ASC

SQL SELECT ORDER BY multiple columns depending on value of other column

I have a table with the following columns:
id | revisit (bool) | FL (decimal) | FR (decimal) | RL (decimal) | RR (decimal) | date
I need to write a SELECT statement that will ORDER BY on multiple columns, depending on the value of the 'revisit' field.
ORDER BY 'revisit' DESC - records with this field having the value 1 will be first, and 0 will be after
If 'revisit' = 1 order by the lowest value that exists in FL, FR, RL and RR. So if record 1 has values 4.6, 4.6, 3.0, 5.0 in these fields, and record 2 has values 4.0, 3.1, 3.9, and 2.8 then record 2 will be returned first as it holds a lowest value within these four columns.
If 'revisit' = 0 then order by date - oldest date will be first.
So far I have the 'revisit' alone ordering correctly, and ordering by date if 'revisit' = 0, but ordering by the four columns simultaneously when 'revisit' = 1 does not.
SELECT *
FROM vehicle
ORDER BY
`revisit` DESC,
CASE WHEN `revisit` = 1 THEN `FL` + `FR` + `RR` + `RL` END ASC,
CASE WHEN `revisit` = 0 THEN `date` END ASC
Instead it seems to be ordering by the total of the four columns (which would make sense given addition symbols), so how do I ORDER BY these columns simultaneously, as individual columns, rather than a sum.
I hope this makes sense and thanks!
In your current query, you order by the sum of the four columns. You can use least to get the lowest value, so your order by clause could look like:
SELECT *
FROM vehicle
ORDER BY
`revisit` DESC,
CASE WHEN `revisit` = 1 THEN LEAST(`FL`, `FR`, `RR`, `RL`) END ASC,
CASE WHEN `revisit` = 0 THEN `date` END ASC
Of course this would sort only by the lowest value. If two rows would both share the same lowest value, there is no sorting on the second-lowest value. To do that is quite a bit harder, and I didn't really get from your question whether you need that.

how can I tell if the last x rows of 'state' = 1

I need help with a SQL query.
I have a table with a 'state' column. 0 means closed and 1 means opened.
Different users want to be notified after there have been x consecutive 1 events.
With an SQL query, how can I tell if the last x rows of 'state' = 1?
If, for example, you want to check if the last 5 consecutive rows have a state equals to 1, then here's you could probably do it :
SELECT IF(SUM(x.state) = 5, 1, 0) AS is_consecutive
FROM (
SELECT state
FROM table
WHERE Processor = 3
ORDER BY Status_datetime DESC
LIMIT 5
) as x
If is_consecutive = 1, then, yes, there is 5 last consecutive rows with state = 1.
Edit : As suggested in the comments, you'll have to use ORDER BY in your query, to get the last nth rows.
And for more accuracy, since you have a timestamp column, you should use Status_datetime to order the rows.
You should be able to use something like this (replace the number in the HAVING with the value of x you want to check for):
SELECT Processor, OpenCount FROM
(
SELECT TOP 10 Processor, DateTime, Sum(Status) AS OpenCount
FROM YourTable
WHERE Processor = 3
ORDER BY DateTime DESC
) HAVING OpenCount >= 10