determine SQL query of albums, and total videos - php

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

Related

Select items where ID present in another column MySQL

I have a table of albums, each album has an ID.
I also have a table of reviews, each review is linked to a corresponding album through the column albumID.
I'm wondering how I can select all albums who's ID's are present in the reviews table.
I was thinking of selecting all albumId's from the reviews column then doing a select where id in but I feel like this would be horribly inefficient.
This is in MySQL
Albums Table
- ID
Reviews Table
- ID
- albumID
Desired result: All albums who have a review. (eg: All albums that have their ID present in the Reviews table)
Here's one approach using a simple JOIN. The inner join notation will only include records that exist in both tables. It allows you to access data in both tables which may be useful if you need data out of reviews too.
Select * from albums A
inner join reviews R
on A.ID = R.AlbumID
This next approach is generally the fastest but you can only get data from albums. This uses what's known as a correlated sub query and is generally the fastest approach.
SELECT * from albums A
where exists (Select 1 from reviews R where A.ID = R.albumID)
and a third approach but generally the slowest... uses a concept called IN(). The sub query here generates a list of ID's from reviews and only shows albums in that list.
Select * from albums where ID IN (SELECT albumID from Reviews)
This would work:
select a.* from albums as a
inner join reviews as r
on r.albumID = a.ID

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

Search by foreign key

I’m working on an application that allows users to search for music, artists, albums and mixtapes.
The DB has these four tables: Music, Artists, Mixtapes, Albums.
The 3 tables; mixtapes, music and albums are related to the artist using foreign keys in a column called artist_id.
It is quite easy to get search results from each of the three tables by using a query such as this:
$music = "SELECT * FROM music WHERE song_title LIKE '%$search_term%'";
However, I’m finding it a bit daunting to get results for albums, mixtapes and music where artist_name = search term since the artist’s reference is not made by name but rather by artist_id
How can I return results for music, albums and mixtapes for a user supplied search term (ie. artist)?
You can achieve this by using an inner join
"SELECT * FROM music INNER JOIN artist ON music.artist_id = artist.id WHERE artist.name LIKE '%$search_term%'";
You need to use a join between the tables, something like this:
select * from music m
inner join artists a on (m.artist_id = a.artist_id)
where a.artist name like ....

selecting id and count(id) from table in mysql

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");

Categories