I want to ask, how to select row value when I set LIMIT 3
here my query :
SELECT
COUNT(tbl_transaction.idimport_database) AS jumlah,
product_name
FROM
tbl_transaction
GROUP BY product_name ORDER BY jumlah DESC LIMIT 3
Result :
I want to showing row query number 2, I marked Yellow.
Thanks
Simplest approach is to select only the second line:
SELECT
COUNT(tbl_transaction.idimport_database) AS jumlah,
product_name
FROM
tbl_transaction
GROUP BY product_name ORDER BY jumlah DESC LIMIT 1,1
You can use offset parameter in the limit clause to provide the offset.
SELECT
COUNT(idimport_database) AS jumlah,
product_name
FROM
tbl_transaction
GROUP BY product_name ORDER BY jumlah DESC LIMIT 1, 1
But as per your data, the above can return either of the first two rows as the value is same for both. You can provide another column in the ordering clause to specify exactly which row you'd like.
Related
so I was trying to use if statement on MySQL. I have a table contains tp1 column and in this column I have two different string values L1_ON or L1_OFF, I want to get L1_ON if there is one on tp1 column but if there is no L1_ON just get L1_OFF. this is what wrote and i know it's not correct just for extra explanation.
$query1 = sprintf('SELECT id,dateandtime,tp1 FROM plate WHERE AND IF(tp1 LIKE "L1_ON") else (tp1 LIKE "L1_OFF") ORDER BY id ASC LIMIT 1');
If "L1_ON" and "L1_OFF" are the only possible values, then you could just sort by tp1 column to ensure "L1_ON" columns come up first:
SELECT
id,dateandtime,tp1
FROM plate
ORDER BY tp1 DESC, id ASC
LIMIT 1
If there could be more than those two values, and you're using this in a script, honestly I would probably just run two queries. One trying to get "L1_ON" values, then a second getting all other values.
SELECT
id,dateandtime,tp1
FROM plate
WHERE tp1 = "L1_ON"
ORDER BY id ASC
LIMIT 1
Finally, if you really want to do this in one query, you could union two queries, one for all "L1_ON" values, followed by one for any that are NOT "L1_ON"
SELECT *
FROM (
SELECT
id,
dateandtime,
tp1
FROM plate
WHERE tp1 = "L1_ON"
ORDER BY id ASC
) AS tp1_on
UNION ALL
SELECT *
FROM (
SELECT
id,
dateandtime,
tp1
FROM plate
WHERE tp1 != "L1_ON"
ORDER BY id ASC
) AS tp1_off
LIMIT 1
using if statement I found the best answer to my question like following:
$query = sprintf('SELECT IF (tp1 REGEXP "L1_ON" ,dateandtime ,"L1_OFF") id,dateandtime,tp1 FROM plate ORDER BY id ASC LIMIT 1 ');
so the code check if tp1 has L1_ON then give me time for that raw, if not just give any L1_OFF raw with time (dateandtime= time of L1_.. raw).
My table before query
My query
<?php $sql="SELECT distinct(SUBSTRING(name,1,instr(name,'(')-1)), pid,sort_order from extra GROUP BY (SUBSTRING(name,1,instr(name,'(')-1)) ORDER BY sort_order ASC LIMIT 0,15"; ?>
Given output
My requirement
So if you want the products with min sort_order that starts with the same 3 lettres
SELECT
ext.*
FROM extra ext
INNER JOIN (
-- this query return the sub_name with the min_order for that sub_name
SELECT
distinct(SUBSTRING(name,1,instr(name,'(')-1)) as 'sub_name',
min(sort_order)
from extra
group by sub_name
) min_extra on SUBSTRING(extname,1,instr(ext.name,'(')-1) = min_extra.sub_name
ORDER BY ext.sort_order ASC LIMIT 0,15";
I got the solution using this query.
SELECT SUBSTRING(pp.name,1,instr(pp.name,'(')-1),(select e.pid from extra as e where SUBSTRING(e.name,1,instr(pp.name,'(')-1)=SUBSTRING(pp.name,1,instr(pp.name,'(')-1) order by e.sort_order asc limit 1) as pid from extra as pp group by SUBSTRING(name,1,instr(name,'(')-1) order by pp.sort_order asc
I want to select last 5 records from first 50 records in the table, currently i have following query, somebody tell me best way to select these records without calculating the limit and offset?
SELECT id FROM table WHERE enabled=1 ORDER BY date LIMIT 5, 45
Try this
SELECT id FROM (SELECT id FROM (SELECT id FROM table ORDER BY id ASC LIMIT 50) AS tbl ORDER BY id DESC LIMIT 5) as tbldata ORDER BY id ASC
this works:
SELECT id FROM (SELECT id,date FROM table ORDER BY date LIMIT 50) AS
temptable ORDER BY date DESC LIMIT 5
I have a question about how to select the second, third, fourth, and fifth largest number in a table. To select the biggest row I use:
$max = SELECT max(money) FROM table
Right now I want to specify $second_max, $third_max, $fourth_max and $fifth_max.
Does someone know how to change my previous SQL select max() easy to specify second max, third max etc...?
I do not want to use:
select money from table order by money desc limit 5;
Because I want them all in different variables.
select money from table order by money desc LIMIT 5
Probably the easiest way is to get them on separate rows:
select t.money
from table t
group by t.money
order by money desc
limit 5;
The next easiest thing is to put them in a comma-separated list:
select group_concat(money order by money desc) as monies
from (select t.money
from table t
group by t.money
order by money desc
limit 5
) T
Just this:
SELECT money
FROM yourtable
ORDER BY money DESC
LIMIT 5
You'll get a 5-record result set, ordered by the top money values - assuming you actually have 5+ records in the table.
USE SQL
select money from table order by money desc limit 5;
The five rows are there as max, secondary,... value of money.
In ORACLE you could do the following :
SELECT *
FROM (
SELECT ADRESSID,
ROW_NUMBER() OVER (ORDER BY ADRESSID DESC) AS ROW_NUM
FROM ADRESSTABLE
) t
WHERE ROW_NUM = 1
OR ROW_NUM = 3
OR ROW_NUM = 5;
i have a MySql table that consists of 2 basic things:
The id and a value.
To show that on my page, i need to select, for example, the last 100 rows on reversed order.
So imagine that someone is putting data on it:
Id, value
1, 10
2, 9
3, 21
4, 15
i need, to select the last "3" rows (LIMIT + ORDER Clause), but not like this: 4,3,2 but like this: 2,3,4.
I know how to do that on code, but maybe there is a simple solution for that on Mysql and i don`t know.
Thanks
My SQL Query is like this right now:
SELECT `Data`.`id`, `Data`.`log_id`, `Data`.`value`, `Data`.`created` FROM `control_panel`.`datas` AS `Data` WHERE `Data`.`id` > 1000 AND `Data`.`log_id` = (2) ORDER BY `Data`.`id` DESC LIMIT 100
You need to wrap the first ORDER BY in a subselect which will return a limited selection ordered in descending order, then you can order that result in the outer query in ascending order:
SELECT
a.*
FROM
(
SELECT id, value
FROM tbl
ORDER BY id DESC
LIMIT 3
) a
ORDER BY
a.id
One way to do this would be with a sub-select.
SELECT *
FROM (SELECT * FROM table_name ORDER BY id DESC LIMIT 3) tmp
ORDER BY id ASC
simply
SELECT t.*
(SELECT * FROM table_name
ORDER BY column_name DESC
LIMIT 0,3) t
ORDER BY t.column_name ASC
use DESC to descending order, ASC to increasing order