I have a "visit" table where views are stored, so every time someone opens my page it adds a row to my database with the post_id - how can I now determine which site (post_id) has the most views?
I have tried
SELECT post_id as idpro, count(*) as count FROM visits group by post_id
but it is not showing me the highest and is not sorted by "showing the highest first"
use order by count DESC limit 1
SELECT post_id as idpro, count(*) as count FROM visits group by post_id order by count DESC limit 1
You need to add an order by count desc To sort the count column with highest values on top
SELECT post_id,
count(*) as c
FROM visits
GROUP BY post_id
ORDER BY c DESC
Try this query
ad as table alias name
SELECT MAX(count)
FROM (SELECT post_id as idpro,COUNT(post_id) count
FROM visits
GROUP BY post_id ) as test;
Related
I have a row with products and I'd like to get 10 random records, but a maximum of 1 row per user_id. Right now I have this:
SELECT user_id, product_id, price, name, category, is_featured
FROM db_products
WHERE category!=15 AND active=1 AND deleted=0 AND is_featured=1
ORDER BY RAND() DESC LIMIT 0,12
I tried doing a SELECT DISTINCT user_id, ... but that doesn't work. The table has 100's of products and each user_id may have multiple ones, but I'd like to retrieve a maximum of 1 per user_id, but still a total of 10.
Is that possible at all without a more complex structure?
I may be missing something, but have you tried doing a GROUP BY?
SELECT user_id, product_id, price, name, category, is_featured
FROM db_products
WHERE category!=15 AND active=1 AND deleted=0 AND is_featured=1
GROUP BY user_id -- SPECIFY FIELD HERE
ORDER BY RAND() DESC
LIMIT 0,12
This will group one row per user, or whichever field you desire to group by.
Try something like that with grouping in main query after random ordering in subquery:
SELECT * FROM
(SELECT user_id, product_id, price, name, category, is_featured
FROM db_products
WHERE category!=15 AND active=1 AND deleted=0 AND is_featured=1
ORDER BY RAND()) AS subquery
GROUP BY user_id
LIMIT 0,10
If you have a column called userID, what query would you use to find the userID with the greatest number of rows in that table?
Thank you
You can use COUNT, ORDER BY that count DESC and LIMIT the result to the top one:
SELECT user_id, COUNT(*)
FROM tableName
GROUP BY user_id
ORDER BY 2 DESC
LIMIT 1;
select user_id, sum(1) as counter
from TABLE
group by user_id
order by 2 desc
limit 1
This should be enough to get only one user, even if more than one user share the maximum amount of rows:
SELECT user_id FROM table
GROUP BY user_id
ORDER BY COUNT(*) DESC
LIMIT 1
If you need to return all matching users:
SELECT user_id FROM table
GROUP BY user_id
HAVING COUNT(*) = (
SELECT COUNT(*) FROM table
GROUP BY user_id
ORDER BY COUNT(*) DESC
LIMIT 1
)
I want to retrieve random rows from table but this rows must be order in category.
select category,
(select order_number
from orders
where order_number in (123,125,128,129,256,263,966,258,264,159,786)
order by rand())
from orders
order by category
This is the query I tried. But that retrieves whole data in table.
Worked query ;
SELECT category,order_number FROM (
SELECT category,order_number
from orders
where order_number in (`$order_numbers_variable`)
order by rand()
) order by category
I assume the requirement is:
Retrieve 'N' random rows from a table sorted by 'category'.
Lets assume N is 10. If you want to change the number of rows, then change it in the LIMIT clause.
SELECT * FROM (
SELECT category from orders ORDER BY rand() ASC LIMIT 10
) AS innerResult
ORDER BY innerResult.category
I want to do a query where I get the top 3 contributing authors i.e. they wrote the most pages / posts. I'd select the data by the session_id of that user associated to each row i.e. a page they wrote. I want to select and order the top 3 people who have the most rows in the DB. How can I query this? I was thinking...
SELECT DISTINCT user_id
FROM music_spot
WHERE (need a condition here)
ORDER BY (the person who contributed the most pages to the third
LIMIT 3
How could I do something like this? Thank you.
SELECT user_id
FROM music_spot
WHERE session_id = 123
ORDER BY count(user_id) desc
group by user_id
LIMIT 3
SELECT user_id, COUNT(post_id)
FROM music_spot
GROUP BY UserID
ORDER BY COUNT(user_id) DESC
LIMIT 0,3
SELECT user_id, COUNT(row_id)
FROM music_spot
GROUP BY user_id
ORDER BY COUNT(user_id) DESC
LIMIT 0,3
I have a table called order_list with the following fields
order_id
user_id
item_id
count
date
order_status
every time users place order, this table used to save order details. so how can I write my SQL query to find top 10 items based on the sum of ordered count?
SELECT item_id, SUM(count) FROM order_list GROUP BY item_id ORDER BY SUM(count) DESC LIMIT 0,10
SELECT *
FROM table_name
GROUP BY item_id
ORDER BY count DESC
LIMIT 10
select item_id, sum(count) as total
from order_list
group by item_id
order by total desc
limit 10