Need help with PHP and MySQL… - php

I have a table of songs, some songs are album song, and some are singles... And I have a table of albums...
Songs table cols: Song_ID, Album_ID, Song_Name, Date_Released, Timestamp_Released, others...
If the Album_ID is [null], it means the song is a single
Albums table cols: Album_ID, Album_Name, Song_IDs, Date_Released, others...
Note 1: there is no Timestamp_Released in albums table
Note 2: Date_Released is only a day with no time i.e. "2011-06-16"
I'm currently using this query to display a table (in my html/php page) that each row is a single or a album (songs that are in an album are displayed all in one row as album)
SELECT
IF(Album_ID IS NULL,s.Song_Name,a.Album_Name) as name,
IF(Album_ID IS NULL,s.Date_Released,a.Date_Released) as datereleased,
s.Timestamp_Released
FROM songs s LEFT JOIN albums a ON (s.Album_ID = a.Album_ID)
GROUP BY 1,2
ORDER BY 2 DESC,1
LIMIT 0,10;
The query above order the list of songs and albums according to date and give the albums the Date_Released and Timestamp_Released of the oldest song in the album...
So my question is how to give the album the Date_Released and Timestamp_Released of the newest song in it ?
Thank you :)

Instead of s.Date_Released, s.Timestamp_Released write MAX(s.Date_Released) as Newest_Date_Released, MAX(s.Timestamp_Released) as Newest_Timestamp_Releasd
UPDATE
SELECT
IF(Album_ID IS NULL,s.Song_Name,a.Album_Name) as name,
MAX(IF(Album_ID IS NULL,s.Date_Released,a.Date_Released)) as datereleased,
MAX(s.Timestamp_Released)
FROM songs s LEFT JOIN albums a ON (s.Album_ID = a.Album_ID)
GROUP BY 1
ORDER BY 2 DESC,1
LIMIT 0,10;

Related

my Sql query to select 2 records per each category

I have 2 tables. 1. news table 2. category table.
in news table i have id, title, category id
in category table i have id and category name.
in news table there are many news items where multiple news items for each category.
what i need was to select 2 record per each category from news table, also i need to join those two tables, so that i can get the name of the category from category table.
i tried using below query
SELECT * FROM (
SELECT
news.id, news.fk_lookups_category, lookups_category.name, news.title, news.description,news.datetime,lookups_category.priority
FROM news
JOIN lookups_category ON news.fk_lookups_category=lookups_category.id
WHERE
news.isPublished='1' and news.datetime >= ('today' - INTERVAL 7 day) order by datetime DESC
) as newitem order by priority

How to get the select the sum of a column JOINing with another table where elementxof table1 = elementx of table2?[special case]

I have two tables named videos and rating.
The first table
videos
uploader video_id
james ac0255
james ue2145
isabell qw2378
The second table:
rating
video_id score
ac0255 4
qw2378 2
ue2145 6
I want to store in variable x the sum of the score of all the videos uploaded by james.
Can anyone suggest an SQL query for it?
Try with this:
SELECT SUM(rating.score)
FROM videos
INNER JOIN rating
ON videos.uploader = rating.video_id
WHERE videos.uploader = 'james';
A simple join will do.
SELECT sum(r.score)
FROM videos v
JOIN rating r ON (v.video_id = r.video_id)
WHERE v.uploader = 'james';
In order to get the sum of score for James , you need to use JOINS and SUM function of mysql
SELECT SUM(Rating.score)
FROM videos as Videos
INNER JOIN rating AS Rating
ON Videos.uploader = Rating.video_id
WHERE Videos.uploader = 'james';

MySQL Multiple Table Query and Results

I am trying to query multiple tables and trying to display the results as follows...
Tables...
news
id title news_datestamp
1 news 1 01/01/2001
2 news 2 01/05/2001
articles title articles_datstamp
1 article 1 01/04/2001
2 article 2 01/06/2001
I'm trying to get a single script to display the results as...
News 1 01/01/2001
Article 1 01/04/2001
News 2 01/05/2001
Article 2 01/06/2001
Any ideas?
You could simple use a Union to merge results in a subselect and sort the final table:
SELECT * FROM (
SELECT id, title, news_datestamp AS datestamp, "news" AS type FROM news
UNION ALL
SELECT articles AS id, title, article_datestamp AS datestamp, "article" AS type FROM articles
) AS temptable
ORDER BY datestamp
The type column will help you to identify id references later.
It looks to me like you want a union of the news and article tables.
SELECT * FROM
( SELECT title,
news_datestamp AS datestamp
FROM news
UNION ALL
SELECT title,
article_datestamp AS datestamp
FROM articles
) AS everything
ORDER BY datestamp, title

Selecting an id with inner join

I'm trying to construct an user profile, so I'm showing all her likes from the database.
But I want to look if the user that have the active session has liked some of the user profile likes.
So, the table name is loves and the structure is:
photo_id (int)
nick (varchar)
date (timestamp)
photos table structure:
photo_id (int)
path (varchar)
title (varchar)
category (varchar)
nick (varchar)
date (timestamp)
This is how I'm traying to do the query:
SELECT photos.photo_id
FROM photos
INNER JOIN loves ON loves.nick = 'userProfileName'
WHERE loves.nick = 'userWithActiveSession'
AND photos.photo_id = loves.photo_id
ORDER BY loves.photo_id DESC
LIMIT 100
This query should return all photo ID's that the user with active session have liked with the liked photos from the profile requested user.
EXAMPLE
loves table:
nick photo_id
userProfile 26
userProfile 1000
userProfile 27
userProfile 520
userSession 26
userSession 680
userSession 1000
So the query should return only two photos_id (1000 and 26), because both users has liked the same photo_id.
Is there any way to modify this code to do what I want?
So you want all the photos owned by X (photos.nick = X) and liked by Y?
SELECT photos.photo_id FROM photos INNER JOIN loves
ON loves.photo_id = photos.photo_id
WHERE loves.nick = Y AND photos.nick = X
ORDER BY photos.photo_id DESC LIMIT 100
If you want photos liked by both X and Y then you need to join loves to itself, matching the photo_ids from the two copies of the table to each other, and conditioning that one table's nick matches X and the other's matches Y. (See comments)
you could get the photo_id without join like this:
SELECT photo_id
FROM loves
WHERE photo_id in (select photo_id from loves where nick = "userProfile" )
AND photo_id in (select photo_id from loves where nick = "userSession" )
GROUP BY photo_id
ORDER BY loves.photo_id DESC
LIMIT 100
DEMO HERE
That part looks weird:
INNER JOIN loves ON loves.nick = 'userProfileName'
shouldn't it be (assuming there is a nick field on photos table):
INNER JOIN loves ON loves.nick = photos.nick
or I didn't get something here?

Inner join with 3 tables

I'm working with PHP and PDO, and I need to recolect information joining 3 tables:
photos
albums
album_photos
The table have the following structure:
photos:
photo_id (int)
path (varchar)
nick (varchar)
date (timestamp)
albums
album_id (int)
album_name (varchar)
nick (varchar)
date (timestamp)
album_photos
album_id (int)
photo_id (int)
nick (varchar)
date (timestamp)
So, I want to show all the albums with a max of 5 photos for each one, where the user nick is 'owner'.
To be shown as follows:
album_name_1:
[photo_1]
[photo_2]
[photo_3]
[photo_4]
[photo_5]
album_name_2:
[photo_1]
[photo_2]
[photo_3]
[photo_4]
[photo_5]
I only know that something like this can be made with INNER JOIN, but I can't found how I can make it with 3 tables.
There examples or other help that I can get to do this?
SELECT a.*, c.date
FROM Album a
INNER JOIN Album_Photo b
ON a.Album_ID = b.Album_ID
INNER JOIN Photo c
ON b.Photo_ID = c.Photo_ID
WHERE c.Nick = 'owner' AND
(
SELECT COUNT(*)
FROM album_photo d
WHERE b.album_id = d.album_id AND
d.nick = 'owner' AND
b.date >= d.date
) <= 2 // <<== change this value to 5
SQLFiddle Demo
I think this can be resolved using Stored Procedures. Using variables and loops, you can retrieve the records you desire. Getting only a maximum of 5 records from an album is quite challenging when you only use the basic SQL commands. Sometimes, you cannot derive the right records. I suggest you use Stored Proc. :)
Try like
"SELECT albums.*,photos.id,photos.path,photo_date as Pht_date
FROM albums
JOIN album_photos
ON album_photos.album_id = albums.album_id
JOIN photos
ON photos.photos_id = album_photos_id
"
But you cont give individual limit for photos where it involved in "JOIN" orelse you need to give individually like
$album_ids = "SELECT album_id FROM albums";
then for the photos you need to write query liks
"SELECT photos.* FROM photos
WHERE album_photos.album_id in (".$album_ids.") AND album_photos.nick = 'owner'
JOIN album_photos
ON album_photos.photo_id = photos.photos_id
LIMIT 0,10
"

Categories