$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`
;
Related
how can i write a sql like this?
$sqlNext = "select * from table WHERE (status='normal') AND (number = (select max(number) from table where number < 5)) LIMIT 1";
in first step filter the result by status column, after that
search in rows with number value less than 5 in status='normal' rows.
in this sql first part of AND=>status='normal' IS NOT WORK
just send me max row with number value less than 5
Isn't this what you're looking for?
select max(number), status from table WHERE status='normal' and number < 5
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.
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
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 want to return only the last 15 rows in my table, then the 15 before that.
Unfortunately while($rows = mysql_fetch_assoc($result)) where the query is SELECT * FROM table returns the data in all rows.
I thought about doing something like:
In my insert script
SELECT * FROM table then $selection_id = mysql_num_rows($result)-14 before inserting any data, then adding column named selection_id which would contain $selection_id, thus each set of 15 rows would have the same selection_id.
In my select script
SELECT * FROM table then $num_rows = mysql_num_rows($result)/15 then SELECT * FROM table WHERE selection_id='$num_rows' and SELECT * FROM table WHERE selection_id='$num_rows-1'.
I could then perform while(..) on both results as usual.
However, I'm not sure this is the most efficient way (chances are it's not), so if not, I'd really appreciate some suggestions to cut down the amount of code I'll have to use :)!!
Use a LIMIT clause in your query, order by your auto-incrementing primary key in descending order. E.g.
SELECT * FROM `table` ORDER BY `selection_id` DESC LIMIT 0,15
...will get the last 15 rows, and:
SELECT * FROM `table` ORDER BY `selection_id` DESC LIMIT 15,15
...will get the 15 rows before that.
Selecting the last 15 rows:
SELECT *
FROM `table`
ORDER BY `id` DESC
LIMIT 0,15
Selecting the 15 rows before the previous ones:
SELECT *
FROM `table`
ORDER BY `id` DESC
LIMIT 15,15
And you can continue in a while cycle.
You need to check out mysql LIMIT. To get the last 15, you'd need to know the number of total rows.
$offset=$rowcount-15;
$sql="SELECT * FROM mytable LIMIT $offset,15";
This is just for example, you'd want to make sure there are at least 15 rows, I'm not sure how mysql would deal with a negative offset. I'll let you figure out how to count the rows.
Edit:
Oh, haha, you could also just sort it descending, that will save you having to query twice.
SELECT * FROM mytable ORDER BY id DESC LIMIT 15;