selecting id and count(id) from table in mysql - php

I am trying to make a photo album system in php and mysql.
I have this code for list all photo albums:
SELECT albums.*, count(pictures.id) AS countpic
FROM albums
LEFT JOIN pictures ON (albums.id=pictures.album_id)
GROUP BY albums.id
ORDER BY albums.date DESC");
I want to list title of photo albums with photo thumbnails (thumb_id si written in table pictures) and also with number of all pictures assigned to each albums.
Dont you know how to select thumb_id and also number of all pictures in album by using one select?
Thanks

Does adding group_concat(pictures.thumb_id) to the select list do what you need?

This would work if for each album there is a single image with is_thumb=1.
SELECT
albums.*,
count(pictures.id) AS countpic,
group_concat(pictures.thumb_id) ids,
group_concat(case when picture.is_thumb then pictures.thumb_id else '' end separator '') thumb
FROM albums
LEFT JOIN pictures ON (albums.id=pictures.album_id)
GROUP BY albums.id
ORDER BY albums.date DESC");

Related

Fetch values from different table according to the value of a field

In my project, I have two types of videos: free videos and featured videos. I am saving the details of free videos in a table channel_videos as shown below:
id videotitle video thumbnail added_on
And I am saving details of featured videos in a table video.
videoid title videolink videothumbnail added_on
When a user views a video the watched videos details along with viewed user details will be stored in another table viewedmovies .
id user_id movie_id videotype watched_on
In this table if the value of field videotype is 0 (free video), then query the table channel_videos, if the videotype is 1 (featured video) then query the table video.
select viewedmovies.id,viewedmovies.watched_on,video.title,video.videothumbnail,video.views,
video.likes,video.length,video.videoid from viewedmovies INNER JOIN video ON viewedmovies.movie_id=video.videoid
and viewedmovies.user_id=1 group by viewedmovies.user_id,viewedmovies.movie_id,viewedmovies.videotype order by watched_on desc LIMIT 6
The above is the query I have used. But this fetches result from video table only.
How can I do this in a single query? Also I want the results to be grouped on movie_id,videotype and user_id fields of viewedmovies table.
Thanks in advance.
I havent tested it, but something like this should work:
SELECT viewedmovies.id, viewedmovies.watched_on
, video.title, video.videothumbnail, video.views, video.likes, video.length, video.videoid
, channel_videos.videotitle, channel_videos.thumbnail, channel_videos.id
FROM viewedmovies
LEFT JOIN video
ON (viewedmovies.movie_id=video.videoid AND viewedmovies.user_id=1 AND viewedmovies.videotype=1)
LEFT JOIN channel_videos
ON (viewedmovies.movie_id=channel_videos.id AND viewedmovies.user_id=1 AND viewedmovies.videotype=0)
GROUP BY viewedmovies.user_id, viewedmovies.movie_id, viewedmovies.videotype
ORDER BY watched_on desc
LIMIT 6

How to select possible row in sql

Alright this is my situation:
I have 2 tables: photos, gallery.
I just want to show all galleries with one photo from each gallery, but I have in photos(TABLE) column title_photo.
I want to show that image, which has title_photo='y' of course some images from other gallery doesn't have it(They have title_photo = 'n'). I have this sql
SELECT * FROM photos
RIGHT JOIN gallery
ON gallery.code = photos.gallery_code
WHERE photos.title_photo = 'y'
GROUP BY gallery.id
I have 4 galleries and in only one I set in row (photos TABLE) title_photo = 'y'. That sql shows only
one gallery, which has in photos(TABLE) set title_photo = 'y'.
My question is:
Is there any solution to show other galleries(they have photos.title_photo = 'n') and show gallery which has title_photo = 'y'?
It seems you want the "first" photo in each gallery, where "first" is defined by the value 'y' in title_photo if that is present. If a gallery has no title photo, we'll use the photo with the smallest photo.id in that gallery as the "first".
For starters, we need to find the pseudo-id for the "first" photo in each gallery. Let's try this:
SELECT photos.gallery_code,
MIN(photos.id + (100000000 * photos.title_photo <> 'y')) AS pseudoid
FROM photos
GROUP BY photos.gallery_code
This gives us either a single genuine photos.id, or one that's one hundred million too large, for each gallery. Next we need to turn that back into a usable photo id, like so.
SELECT gallery_code, (pseudoid MOD 100000000) AS id
FROM (
SELECT photos.gallery_code,
MIN(photos.id + (100000000 * photos.title_photo <> 'y')) AS pseudoid
FROM photos
GROUP BY photos.gallery_code
) AS firsts
Next, we need to join that list of first photos to the photos and gallery table.
SELECT p.whatever, g.whatever
FROM (
SELECT gallery_code, (pseudoid MOD 100000000) AS id
FROM (
SELECT photos.gallery_code,
MIN(photos.id + (100000000 * photos.title_photo <> 'y')) AS pseudoid
FROM photos
GROUP BY photos.gallery_code
) AS firsts
) AS f
JOIN gallery AS g ON f.gallery_code = g.code
JOIN photos AS p ON f.id = p.id
ORDER BY (whatever you need here)
See what we're doing here? We're using a query to generate the gallery/first-photo relationship using the specified logic.
Then we're employing that as a subquery (virtual table). We're joining it to the two data tables to retrieve the information we need, just for those first photos.
Pro-tip: Don't use SELECT * in application software. Be specific about the columns you need in your result sets.
Pro-tip: Don't use the nonstandard MySQL extension to GROUP BY unless you're really expert. http://dev.mysql.com/doc/refman/5.5/en/group-by-extensions.html

PHP order by another table with added count

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

determine SQL query of albums, and total videos

For the query i need to do, a user uploads videos onto an album, on their video page, it has the list of albums, and the total of videos below it.
Currently, what i am trying to do is a union, however this is not working, this is my code:
$query="SELECT albname, albcover, lid, NULL as vidcount FROM albums WHERE USERID='".mysql_real_escape_string($USERID)."' and verified='1'
UNION
SELECT NULL as albname, NULL as albcover, NULL as lid, count(*) as vidcount FROM albs_vids A LEFT JOIN albums B USING(ALBID) WHERE B.USERID='".mysql_real_escape_string($UID)."'";
So, what happens is the SQL query needs to find the data of the album, ie albname, albcover and lid from the table albums. Then find the amount of videos in the table albs_vids which correspond to the album. The album is specified by ALBID in both tables.
Hard to completely understand what you're asking ,but this will give you the count of videos for each album that has your userid:
$sql = "SELECT a.albname, a.albcover, a.lid, COUNT(*) AS vidcount
FROM albums AS a
LEFT JOIN albs_vids AS b ON b.albid = a.albid
WHERE b.userid='".mysql_real_escape_string($UID)."'
GROUP BY a.albname";

need starting point on an ORDER BY

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

Categories