I'm searching a very large database and would like to limit the rows returned based on this criteria.
My query looks like this
SELECT id,value,year FROM table WHERE value = '$formvariable'
All I really want is this:
If more than 250 rows are returned by the query show only the last 3 years of results ordered by year descending, limit 1000.
Otherwise 250 rows or less, show all.
You can try doing something like this:
SELECT * FROM YourTable
WHERE value = '$formvariable'
AND 250 >= (SELECT COUNT(*) FROM YourTable)
UNION ALL
SELECT * FROM YourTable t
WHERE value = '$formvariable'
AND 250 <= (SELECT COUNT(*) FROM YourTable)
AND t.year >= YEAR(DATE_ADD(curdate(), INTERVAL -3 YEAR))
ORDER BY t.year DESC
LIMIT 1000
In case there are less then 250 records, all will be selected in the first part of the UNION, and none will be selected from the second.
In case there are more, none will be selected from the first part, and only the 1000 first records of the past 3 years ordered by year will be selected.
Related
I need help to implement how much status=1 I have in last 10 rows from results?
If its there 3 status in last 10 rows, i need to got 3 output.
SELECT * FROM results WHERE $position='$text' and make='$make' and status='1'
You need to use a subquery to get the last 10 rows, and then count how many have status = 1 in that subset.
SELECT COUNT(*) AS count
FROM (SELECT status
FROM results
WHERE $position = '$text' AND make = '$make'
ORDER BY id DESC
LIMIT 10) AS last10
WHERE status = '1'
DEMO
$query = "SELECT sum(DAILYTIME) FROM $mytable LIMIT 5";
that statement selects and sums limited to 5 rows, but how to do it based on certain row. For example I want to use row #10 as a starting point and sum 5 rows below that one ( 5 rows including that same row ).
how would the statement need to be, because if i do WHERE DAY = '$day' LIMIT 5
it will not sum all the 5 rows, just 1
By adding a where clause with the >= operator, you will also select all following rows. Use the following query:
$query = "SELECT sum(DAILYTIME) FROM $mytable WHERE DAY >= '$day' LIMIT 5";
This will give you the sum of the 10th through 14th "rows"; however, since there is no ORDER BY, there is no guarantee which rows of the table the result rows of the subquery would be.
SELECT sum(DAILYTIME)
FROM (
SELECT DAILYTIME
FROM $mytable
LIMIT 9, 5
) AS `rows10to15`
;
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 column result in my table and want to know how many rows are affected where result contains a specific number like '3' in the last 25 rows before a specific id.
Eg: Want to know how many rows have a result = 3 of the 25 rows before the id "500".
What is the most efficiƫnt way to reach this in Php and MySQL.
You can do that completely with SQL:
SELECT COUNT(x.result)
FROM (
SELECT *
FROM table
WHERE id < 500
ORDER BY id DESC
LIMIT 25
) x
WHERE x.result = 3
First you find all entries that are relevant to your query by specifying the search criteria. Then you limit this search to 25 items and sort it in reverse order. That should yield the 25 last elements. Finally you just do your COUNT() for items with a result value of 3.
SELECT COUNT(result)
FROM table
WHERE id < '$specificID'
AND result = 3
ORDER BY id DESC
LIMIT 25
Try this
SELECT count(*) FROM `your_table`
WHERE result LIKE '%3%'
AND id < 500
ORDER BY id ASC
LIMIT 25;
I have this Query :
select
id_re_usr,
year(time) as AYear,
DAYOFYEAR(time) as ADay,
DATE_FORMAT(time, "%m-%d-%y") as date,
count(*) as TotalPerDay
from numrequest
where id_re_usr = "u1"
group by id_re_usr, AYear, ADay
order by AYear, ADay
it outputs something like
date TotalPerDay
------------------------
01-01-87 1
01-09-12 5
02-09-12 17
03-09-12 1
how can I find the maximum TotalPerDay without changing the current output by using php or changing the query.
I tried to do this and it works
$max=0;
while($row=mysql_fetch_array($results)){
if($max<$row['TotalPerDay']){ $max= $row['TotalPerDay'];}
}
but isn't there a direct way to do it?
if modifying the query the output should be
date TotalPerDay max
----------------------------------------
01-01-87 1 17
01-09-12 5 17
02-09-12 17 17
03-09-12 1 17
Join it to a second query of just the max count.. The inner-most queries on a per day basis (for the given user) a set of rows on count grouped per day. From that, the next outer does a select MAX() from that set to find and get only one record representing the highest day count... Since it will always return a single row, and joined to the original numRequest table it will be a Cartesian, but no problem since its only one record and you want that value on every returned row anyhow.
select
id_re_usr,
year(time) as AYear,
DAYOFYEAR(time) as ADay,
DATE_FORMAT(time, "%m-%d-%y") as date,
count(*) as TotalPerDay,
HighestCount.Max1 as HighestOneDayCount
from
numrequest,
( select max( CountsByDate.DayCount ) Max1
from ( select count(*) as DayCount
from numrequests nr
where nr.id_re_usr = "u1"
group by date( nr.time )) CountsByDate
) HighestCount
where
id_re_usr = "u1"
group by
id_re_usr,
AYear,
ADay
order by
AYear,
ADay