I have a simple question. In MySQL, consider a row "n", how can we order rows by id (for example), but start from the row "n+1" and end to the row "n-1" ?
Thanks !
EDIT : I ommit to precise that I seek the query in MySQL.
From an answer below, here an example :
ID
---
1
2
3
4 <--N
5
6
I want Desired Results ordered as follows
5 <--N + 1
6
1
2
3 <--N - 1
So you mean. For a table
ID
---
1
2
3
4 <--N
5
6
You want Desired Results ordered as follows?
5 <--N + 1
6
1
2
3 <--N - 1
If so
SELECT ID
FROM T
WHERE ID <> 4
ORDER BY CASE WHEN ID > 4 THEN 0 ELSE 1 END, ID
Assuming table MyTable with integer column N:
SELECT *
from MyTable
where Id between N-1 and N+1
order by N desc
You're asking how to sort by descending ?
Just stick
ORDER BY col a , col b DESC;
at the end
Related
can anyone help me to group every 3 counts rows???
I have data like this
num
score
1
3
1
3
1
3
1
3
1
3
1
3
4
3
4
3
4
3
2
3
2
3
2
3
and i want result like this
num
count(num)
1
3
1
3
2
3
4
3
You want to group every 3 rows. But without giving supplementary conditions, we can only take assumptions on how you would like to group. Supposing each row in the 3-rows group has the same content and the score value from the base table has no influence on grouping, I guess you just want to get the num once for every 3 rows as the 3 rows have the same num. Besides, we use a constant value 3 for count(num) column. If all my assumptions come true, try this:
select num, 3 as 'count(num)'
from (select num,#row_id:=#row_id+1 as row_id
from test,(select #row_id:=0) t1 ) t2
where row_id mod 3 =0
order by num
;
I have a strange error first time seing such results maybe someone else also found how to overpass such odd error.
I have a table structure like this :
|id|uid|test1|test2|rid|
1 1 - - 1
2 1 - - 1
3 2 - - 2
4 2 - - 2
5 3 - - 3
6 3 - - 3
7 4 - - 4
8 4 - - 4
----------------------------
as "-" are varchar data all other columns are integers
My query with the odd results is this :
SELECT COUNT(uid) AS COUNT,
id AS ID,
uid AS InqID,
test1 AS A,
test2 AS B
FROM test_table
WHERE rid = (X)
GROUP BY uid
ORDER BY id DESC
As results i get
if (X) = 1 or 3 it shows me the correct last id number (2 id for 1 and 6 id for 3)
but if (X) is 2 or 4 it shows me the first in row id number (3 id for 2 instead of 4 and 7 id for 4 instead of 8)
Can anyone tell me why i get the correct results only as singles in rid and not at even numbers of rid column or how at least this query will work as it has to ?
Thank you all in advance
Use MAX(id) instead of ORDER BY.
SELECT COUNT(uid) AS COUNT, MAX(id) AS ID, uid AS InqID, test1 AS A, test2 AS B
FROM test_table
WHERE rid = (X)
GROUP BY uid
Suppose I have a table like so,
unqiue_data int(10),
not_unique_data int (10)
unique_data not_unique_data
1 1
2 1
3 2
4 2
5 2
select * from some_table order by not_unique_data DESC;
What I need to do, is randomize this SELECT query, but in a very two particular ways that I just can't figure out how to do. Firstly, I want unique_data randomized, so that the SELECT query could return something like (randomly):
unique_data not_unique_data
2 1
1 1
4 2
3 2
5 2
The second requirement I have is that, unique_data appears multiple times, but in a very specific order.
In an ideal world, I need is so that it could return something like
unique_data not_unique_data
4 2
3 2
5 2
1 1
2 1
3 2
5 2
4 2
2 1
1 1
5 2
4 2
3 2
What I mean by this is, I need it so that each unique_data (4,3,5), (3,5,4), (5,4,3) The first number of each set appears only once while still being ordered by not_unique_data.
How to do this?
Well for this problem you have to make sure that 100 products related to a product
how many of them have appeared for that product
how many of them will be appeared for that product
We can use a temporary table to do so
SELECT unique_data, not_unique_data, 0
INTO temp_newtable
FROM some_table
ORDER BY RAND()
Now we will get a randomly organized table and by default seen=0 (seen to know it has been appeared for that product or not)
unique_data not_unique_data seen
4 2 1
3 2 1
5 2 0
1 1 0
2 1 0
3 2 1
So whenever some product related to product appear on page you need to update seen column to 1, when you are out of this table truncate and generate random data for usage again
I think you are looking for this https://stackoverflow.com/a/3990479/2552551
SELECT * FROM
(
SELECT * FROM some_table
ORDER BY not_unique_data DESC
LIMIT 100
) T1
ORDER BY RAND()
I am doing a sorting on a table to show the records with status having 3 first then status having 4 and then 1.
in current table this is the output
id status
1 3
2 4
3 4
4 3
5 1
now when i apply the query
select * from table order by model.status desc
the output is:
id status
2 4
3 4
1 3
4 3
5 1
what i want actually the below output. first with status 3 then with status 4 and then with status 1. How to achieve the below output
id status
1 3
4 3
2 4
3 4
5 1
instead of a magic 9999999 number, you can use ~0, which represents the maximum possible value for big int... should be enough ;)
order by (case when status = 1 then ~0 else status end)
other solution, without any magic number
order by status = 1 , status
which will sort by a "boolean" (0 or 1 in DB) first, then by status
You can use FIELD to do that easily on MySQL;
SELECT *
FROM model
ORDER BY FIELD(status, 3, 4, 1);
SQLfiddle here.
If FIELD is not working for you (perhaps due to older version of MySql) then try this. It will work
SELECT * FROM model
ORDER BY CASE status WHEN 3 THEN 1 WHEN 4 THEN 2 WHEN 1 THEN 3
END, id
Check it working on this link
Following should work
ORDER BY CASE status WHEN 3 THEN 1
WHEN 4 THEN 2
WHEN 1 THEN 3
END, id asc
Use SQL FIELD
SELECT * FROM `table` ORDER BY FIELD (status, 3, 4, 1);
$invoiceList = InvoicesModel::where('hotel_id', $hotel_id)
->where('status', 'PENDING')
->orWhere('status', 'COOKING_PROCESS')
->orderBy(
DB::raw('(CASE WHEN status = "COOKING_PROCESS"
THEN 1 WHEN status = "PENDING"
THEN 2 END)')
)->get();
Use "FIELD()" in order by clause if status values are 1,3 and 4 only.
select * from table order by FIELD( model.status 3,4,1)
I am looking to do the following:
tblOne:
-page_id
-split
tblMany
-view_id
-page_id
I want to order tblOne by the number of related page_id in tblMany.
Then divide the number of rows in tblOne by 5 and update tblOne.split to a number between 1 - 5 into which split it falls... e.g if there are 50 rows... row 0 - 9 are split 1, 10 - 19 split 2...etc
I am sure I can do the 'count' part... but havn't a clue how I would update the 'split' row
This query will give you the distinct page id and their count ordered by page_id count in descendant order (so max count first) :
SELECT page_id, count(1)
FROM tblMany
GROUP BY 1
ORDER BY 2 DESC
So for a tblMany like this:
view_id | page_id
--------+---------
1 1
2 1
3 2
4 2
5 2
6 3
You will get
page_id | count(1)
--------+---------
2 3
1 2
3 1