Count how much matches in last 10 rows - php

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

Related

how to count rows starting at specific position

I want to count all rows that infront of a specific ID, so I can output how many entries are infront of the users entry.
I have no clue how to count the rows, starting at the ID of the user (for ex. ID 10).
How to count from ID 10 to ID 1 where status = unpublished
Thx
Are you looking for something like-
SELECT COUNT(*)
FROM your_table
WHERE ID BETWEEN 1 AND 10
AND STATUS = 'unpublished'
Try This --
SELECT COUNT(*)
FROM tablename
WHERE ID BETWEEN 1 AND 10
AND STATUS = 'unpublished'
ORDER by ID DESC
SELECT * FROM
WHERE ID = 10 AND status='unpublished';

Select only 5 random rows in the last 50 entries

I'm just starting with MySQL so I would like to know how can I select only 5 random rows in the last 50 entries of my database? I hope you understand my question.
I'm using PDO and what I have now is this:
$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);
Then I use a PHP foreach loop...
Thank you
The challenge is determining the last 50 entries. Assuming you have an auto-incremented id, you can do:
SELECT a.*
FROM (SELECT a.*
FROM articulos a
WHERE cat = '$ArtCat'
ORDER BY id DESC
LIMIT 50
) a
ORDER BY RAND()
LIMIT 5;
The key idea is the subquery to get the last 50 entries, and then the final query to get the 5 random rows. The subquery needs to specify how you identify the last 50.
$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() LIMIT 5 ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);
just add limit
i assume in your table you have some date column so we can get last 50
SELECT * FROM (SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY created_tiem desc limit 50 ) t order by RAND() limit 5;

Variable limit on dataset using PDO

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.

Most efficient way to calculate affected rows with a limit

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;

How to get the value of last mysql fetch data? PHP-MySQL

I want to do this:
Check the top 10 values of points.
Here is my condition:
If there are less than 10 records (rows) found, e.g give bonus
If the points is in top ten (among hundreds records/rows), give bonus.
So, what i do is (wrong method):
SELECT points FROM `scores` WHERE id = '1' ORDER BY score DESC LIMIT 9 , 1
That will only work if i have more then 9 (at least 10) data/records.
Is there any other way?
I am thinking of using this(not very good though):
SELECT points FROM `scores` WHERE id = '1' ORDER BY score DESC LIMIT 0 , 10
Then get the last value of mysql_fetch_assoc data. Thus, how do I get the last value of mysql_fetch_assoc data?
The previous query is close to what you want:
SELECT points FROM (
SELECT points, score
FROM scores
WHERE id = '1'
ORDER BY score DESC
LIMIT 10
) AS top_ten
ORDER BY score ASC
LIMIT 1
If you want to stick with mysql_fetch_assoc getting the last value you can utilize mysql_data_seek.
Something like this:
<?php
// ... $result is the result set from your query
$result_count = mysql_num_rows($result);
if ($result_count > 0)
{
mysql_data_seek($result, $result_count - 1);
}
$row = mysql_fetch_assoc($result);
?>
try to use this:SELECT points FROM (SELECT points FROM scores WHERE id = '1' ORDER BY score DESC LIMIT 10) ORDER BY score LIMIT 1

Categories