how to count rows starting at specific position - php

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';

Related

Count how much matches in last 10 rows

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

how to count all the rows of a table starting from specific row

how to count all the rows of a table starting from specific row by id?
like
SELECT COUNT(*) FROM aptec ORDER BY id ASC AFTER id = 1000;
is that possible ?
Try
SELECT COUNT(*) FROM aptec WHERE id > 1000

count the number of times a value appears in a column where liked_id matches likes?

does anyone know how you would count the total number of times a value appears in a single column.
for instance my table looks like:
user_id | liked_id | likes
3 1 1
4 1 1
what i want to do is count the total of the likes column where liked_id matches.
So liked_id 1 has 2 likes?
can someone please show me how i might do this?
function count_likes() {
global $connection;
global $profile_id;
$query = "SELECT likes, count(*) FROM ptb_likes GROUP BY liked_id";
$count_likes_set = mysql_query($query, $connection);
confirm_query($count_likes_set);
return $count_likes_set;
}
Try this:
SELECT liked_id, SUM(likes) FROM ptb_likes GROUP BY liked_id
you should grouped them by liked_id
SELECT liked_id, count(*) totalLikes
FROM ptb_likes
GROUP BY liked_id
if you just want the number of rows with like_id of 1,
SELECT liked_id, COUNT(likes) FROM ptb_likes WHERE liked_id = 1;
If you want the the frequency of each like_id:
SELECT liked_id, COUNT(likes) FROM ptb_likes GROUP BY liked_id;
As a note: don't use SUM as that adds all the values of like_id, and if you have a like_id greater than 1, your count will be wrong. Use count instead.

Select a row where one column is min

I'm writing a simple URL rotator for a client and they want the URLs to rotate according to the oldest one that was previously displayed.
My columns are very simple:
url_id | company_id | url | last_clicked
I want to fetch a single row where the company_id is passed in and the last_clicked is the minimum of all records matching the company_id.
It should also select a random url_id if all last_clicked values are empty.
I assume this can be accomplished with a GROUP BY and HAVING but I can't seem to get the query to return anything.
I have this:
$last = $this->db->fetchOne ("SELECT url_id FROM
{$this->prefix}urls GROUP BY company_id HAVING MIN(last_clicked)
WHERE company_id='$company'");
This will fetch the row where last_clicked is smallest, or at random if they are all NULL:
SELECT url_id FROM urls
WHERE company_id = $company
ORDER BY last_clicked, RANDOM() LIMIT 1;
An index based on company_id and last_clicked would greatly help:
CREATE INDEX urls_ndx ON urls(company_id, last_clicked);
SELECT url_id FROM
TABLE
WHERE company_id='$company'
AND last_clicked = ( SELECT MIN(last_clicked)
FROM TABLE WHERE company_id='$company' )

PHP / MySQL - Getting last 3 uniquely updated records

Im wondering if someone could help out.
I need to write a query that gets the last 3 'created' records, but their UID has to be unique so for example, my mysql fields look like so
uid created
19 2012-02-01 01:08:43
18 2012-02-31 17:07:21
19 2012-02-31 16:07:20
20 2012-02-31 13:03:00
Ok, so i want to get the last 3 uid's created ... but they have to be unique uid's so the same uid cant appear twice.
Cheers
this should do this
SELECT * FROM ( SELECT * FROM tablename ORDER BY uid, created DESC ) ordered GROUP BY uid
You can select last 3 IDs with this query:
SELECT * FROM ( SELECT * FROM table ORDER BY uid, created DESC ) AS selected GROUP BY uid LIMIT 3
Logic is: order table by uid field in descending and limit to 3 fields.
Use a Distinct and Group by to get what you're after:
SELECT DISTINCT * FROM table GROUP BY uid;
That will give you all unique UID's.
Then add your ORDER BY in there to make sure you grab the last records.
SELECT *
FROM table
ORDER BY created DESC
GROUP BY uid LIMIT 0,3
SELECT uid
, MAX(created) AS max_created
FROM tableX
GROUP BY uid
ORDER BY max_created DESC
LIMIT 3

Categories