How do order by another table's column and get the count from other table column so I can order by that.
For example:
$sql="SELECT * FROM photos, views WHERE photos.unqid = views.photoid ORDER BY CAST(views.id AS SIGNED)";
I want to add the views to the photos. So that photos are order by the views.
Something like the following
SELECT p.*, v.*, COUNT(v.photoid) as nb_views
FROM photos p, views v
WHERE p.unqid = v.photoid
GROUP BY v.photoid
ORDER BY nb_views
Related
I have a article pages, that i only want to show feed of the users that every account is following, kinda like twitter.
I have to distinct tables one is called posts other is called followers
I want to loop the articles but i want it to select only the users that my account is following.
I just need the SQL query that can do that since the PHP part of the rest i can perform it
Something like this , although i know that this one does not work
$connect->query("SELECT id,userid,postagem FROM posts WHERE IN (SELECT followed FROM followers WHERE whofollowed = '$userd' ) ORDER BY id DESC LIMIT 13");
Assuming followed is also a userid in the followers table:
$connect->query("SELECT id,userid,postagem FROM posts WHERE userid IN (SELECT followed FROM followers WHERE whofollowed = '$userd' ) ORDER BY id DESC LIMIT 13")
I'm trying to select posts that are posted by somebody the user is following.
My posts table looks like this:
My followers table looks like this:
The "creator" column in the posts table is the ID of the user who posted it. The "follower" colum in the followers table is the user who is following the "following" column.
I'm using this query (to no avail) to select all from posts where creator=all users the user is following:
SELECT * FROM posts WHERE creator=(SELECT following FROM followers
WHERE follower=the user's id) ORDER BY datetime DESC;
In theory:
SELECT * FROM posts WHERE creator=1 or 2 or 3 or 4...or 998 or 999
etc, etc.
What would be the best way to do this?
Thanks!
Try changing the query to:
SELECT * FROM posts WHERE creator in (SELECT following FROM followers WHERE follower=the user's id) ORDER BY datetime DESC;
Using joins in this case can boost up the performance of your query. I recommend the following:
SELECT * FROM posts p
INNER JOIN followers f
ON p.creator = f.following
WHERE f.follower = <user_id>
ORDER BY datetime DESC;
Try it
SELECT *
FROM posts p, followers f
WHERE
p.creator=f.following
and f.follower ='user_id'
ORDER BY p.datetime DESC;
I have two tables in my SQL database, one for the photos that users sent and another one for the votes which photo had on my application. I need to extract the 30 photos from the 'photos' table which had the most votes on the 'votes' table.
Is there a way to do it within a single query?
You should be able to use a query like this:
select
a.photoFileName
from
photos a
join votes b
on a.photoId=b.photoId
order by
b.voteCount desc
limit 30
Adjust the keys to your exact column names on the linked fields.
This assumes that the votes table has an number column (voteCount) that has a tally of the votes for that image.
Something like this ( if each vote is stored single ), but make your own adjustments:
SELECT
p.id,
COUNT( v.id )
FROM
photos p
JOIN
votes v ON p.id = v.photo_id
ORDER BY
COUNT( v.id ) DESC
GROUP BY
v.photo_id
LIMIT 30;
PS: I did not test the query, just gave you an example!
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
The website contains images. These images can be ranked. When an image is ranked, the value can be 1,2, or 3. To save ranking I have a table ranking_items. The images are displayed as thumbnails. The boss would like me to order them by rank. the problem is, how do I also include images in the result with no entry in the ranking_items?
$db->query("SELECT file_name
FROM images, ranking_items
WHERE images.id=ranking_items.image_id
ORDER BY ranking_items.rank ASC");
When you do FROM table1, table2 you are doing a JOIN.
Try to LEFT JOIN the ranking_items table. This will return all rows, and put NULLS in the places where the join fails.
SELECT file_name
FROM images
LEFT JOIN ranking_items ON images.id=ranking_items.image_id
ORDER BY ranking_items.rank ASC
SELECT file_name
FROM images LEFT JOIN ranking_items
ON images.id=ranking_items.image_id
ORDER BY ranking_items.rank ASC