Order results by results from a different table in one query - php

Is it possible to have something like this in one query,
Count how many likes a certain ID has in image_likes and then order results from images descending by how many likes they have.

SELECT
*
FROM
`images`
ORDER BY
(SELECT COUNT(`id`) FROM `image_likes` WHERE `image_id`=images.`id`) ASC
(I have of course made up field names, but this format should work)
If possible, you might want to change the way the system works so that you can just read the total likes from a field name rather than doing a subselect.

Untested
select imageid, count(imageid) from image_likes
Group by imageid
Order by Count(imageid) desc

select * from (SELECT *,(SELECT COUNT(*) as count from image_likes il WHERE ID = i.ID)
FROM images) tbl ORDER BY COUNT
untested

Related

How to execute another query in a running query after a specific number of records using php?

I searched a lot but didn't found the exact result that I need. I have a project of question and blog website. There are two tables question_table and blog_table, now I want to execute both in two different queries and ORDER BY date on one page as newsfeed. I'm sorry if I didn't explain well.
Attached Image for detail description (result must like this)
My sql query "SELECT * FROM blog_table WHERE status=1 ORDER BY posted_date DESC"
and same for question_table
"SELECT * FROM question_table WHERE status=1 ORDER BY posted_date DESC"
Please help me to findout the exact result in the image bellow.
How can I run this query:
select posted_at,user_id,post_title,post_data from blog_table
UNION
select asked_at,user_id,question_title,question_slug from question_table
order by date desc
Your ORDER BY only applies to the question_table query, not the whole thing. You need to put the UNION in a subquery so you can order the entire result.
SELECT *
FROM (
select posted_at AS date, user_id, post_title AS title, post_data AS content
from blog_table
UNION
select asked_at AS date, user_id, question_title AS title, question_slug AS content
from question_table
) AS x
order by date desc

Mysql SELECT statement order by count

I am not sure if this is possible or not
I have a table with orders and userid
I need to do a SELECT query with ORDER BY the least number of orders of a customer
Example:
userid1 has 10 orders
userid2 has 5 orders
Then SELECT should show userid2 at the top
Thanks in advance
If you have orders as ordersCount i mean as a field which keeps counts of orders then
Select * FROM YourTable order by orders ASC
Else if you have a record for each order then
Select * FROM YourTable Group by UserID order by count(*) ASC;
Apply limit like this
Select *
FROM YourTable
Group by UserID
order by count(*) ASC
limit startIndex, fetchSize;
where startIndex and fetchSize are int that define your limit
You need to group by the userid so you can count orders by user.
SELECT userid, count(1) FROM orders GROUP BY userid ORDER BY count(1) asc
You can accomplish that using GROUP BY and ordering by COUNT().
It would be something like that.
SELECT userid, COUNT(*) qt
FROM orders
GROUP BY userid
ORDER BY qt
If each order is in its own row, however, you need to aggregate them:
SELECT * from MYTABLENAME GROUP BY userid ORDER BY COUNT(*)
Count is what you're looking for
SELECT count(theField) as theCount, *
FROM theTable
GROUP BY theField
ORDER BY theCount
You can select the COUNT of a column and give it a name, then use that name to sort by. Here's an example:
SELECT
p.*,
(SELECT COUNT(o.order_id) FROM orders AS o WHERE p.partron_id=p.patron_id) AS orderCount
FROM
patrons AS p
WHERE
...
ORDER BY
orderCount ASC
UPDATE: Just realized this is only useful if you have orders in a separate table than patrons. Disregard if you are only looking at a single table.
This query expects you to have two tables, a patron table with patron information like name, and a orders table that has a patron_id column that matches the identity column in the patron table.
The advantage to doing this is that you can request patron information at the same time, so you don't need to run two queries in PHP if you're going to use that information.

MySQL - How to select random after select top?

I have a query like this:
SELECT Id, Name, image, price, view FROM estore.product ORDER BY view DESC LIMIT 9
and I want select random 5 records in that query. I tried but this code doesn't work:
SELECT Id, Name, Image, Price, View FROM (
SELECT Id, Name, Image, Price, View FROM estore.product ORDER BY View DESC LIMIT 9)
ORDER BY RAND() LIMIT 5
How can I do? Thanks for watching?
A subquery must be named. Try:
LIMIT 9) as SubQueryAlias ORDER BY RAND()
^^^^^^^^^^^^^^^^^^
You may want to go and read this thread
Multiple rows alternative for RAND()
If your table is quite large (and it probably can end up quite large being a product table) the rand() limit is quite a slow query
SELECT Id, Name, Image, Price, View FROM estore.product ORDER BY RAND() LIMIT 5
(Using subquery for order and limit the same selection is madness...)

How can I display the article of a specific user according to the number of views of this article?

I have a table that contains articles posted by users. The structure of the table is like this:
id uid author article_name article num_views edit_time
id is the article id, uid is the author id and num_views is how many times this article is being viewed.
What I need is to display ONLY the most viewed article according to uid
In short , if uid is author id and article id is 5 , what I need is to display something like :
id uid article_name num_views
5 1 article title 100
I tried to run several queries in Mysql but i could not figure it out , can anyone help please ??
The most viewed article and its count by each user:
SELECT `id`, `uid`, `article_name`, `num_views`
FROM `table`
WHERE `num_views` IN (
SELECT MAX(`num_views`) FROM table GROUP BY `uid`)
For only given uid
SELECT `id`, `article_name`, MAX(`num_views`)
FROM `table`
WHERE `uid` = <user id>
If you need only one entry you should try to sort your query with ORDER BY command and limit it to the one article by LIMIT 1 command.
SELECT * FROM ARTICLES WHERE (some criteria) ORDER BY NUM_VIEWS DESC LIMIT 1.
That should select an article with the most views.
It's a bit hacky and if your table gets big it's going to be non-performant, but one way to do this would be:
select uid,
group_concat(id order by num_views desc) as most_viewed_id,
group_concat(article_name order by num_views desc) as most_viewed_name,
group_concat(num_views order by num_views desc) as num_views
from yourtable group by uid
You'll then have to parse out the first element from each of the group_concat'd fields.
Another way would be to use a join with a subquery, but it can be weird if there's two articles for the same user with the same highest number of views, and also has performance issues:
select uid,num_views,id,article,article_name from
yourtable join
(select uid,max(num_views) as maxviews from yourtable group by uid) as subtable
using (uid,num_views)
Of course at a certain point the best way might be to just iterate over uids with a simple order by query. It's a lot more queries but it can be faster depending on your data. Unfortunately SQL just isn't really set up to do queries like this.

MYSQL position of row by order

I have had a look through google results, but can't find a simple answer, I have a table
id, points (and more fields, but they dont affect anything)
How would I go about Getting position of record 24(id) ordered by points DESC?
I'm not sure I've understood your question
select * from table order by points desc limit 23,1
Select Count(*) from (
Select ID from UnNamedTable A where A.points>(Select B.Points from UnNamedTable B where B.ID=24)
)
Are you trying to figure out out what place a user is in based on a points system?
If so then just find how many users have more points than a particular user then add 1.
SELECT COUNT(*) + 1 AS 'position' FROM table WHERE points >
( SELECT points FROM table WHERE id = someIDhere )
You could create a variable to keep track of your row position after sorting. Something like this would work:
select #rownum:=#rownum+1 ‘rank’, p.* from table1 p, (SELECT #rownum:=0) ID order by Points desc limit 10;

Categories