Select the result instead of for loop in PHP using MYSQL - php

There are two table :
user_playlist
---------------
id
user_id
playlist_id
title
description
playlist
-------------
id
user_playlist_id
video_id
And I would like to get the data:
All the user_playlist of a specific user, and for each playlist , check whether the video_id exist
Right now I use php to get user_playlist first and foreach user_playlist count whether the video_id is exist in playlist.
It works but seems not efficient, hope can done by a SQL Query.
Thanks for helping.
Update:
**Sample data**
user_playlist
1, 1, 1, list_1_title, list_1_description
2, 1, 3, list_5_title, list_5_description
playlist
1, 1 , 5
2, 1 , 4
3, 2 , 6
for example , a user id #1 and video #4 result is (using up as alias of user_playlist) :
up.id up.title up.description up.user_id is_video_id_select
1 list_1_title list_1_description 1 1
3 list_5_title list_5_description 1 0

Yes this can be done using simple left join
SELECT up.*,pl.id, pl.user_playlist_id,
(
CASE WHEN pl.video_id IS NULL THEN 0 ELSE 1 END AS is_video_id_select
)
FROM user_playlist as up
INNER JOIN playlist
ON up.playlist_id = pl.id
WHERE up.user_id = 'x' // where x is a user id
this will bring the common data between both the table
for more see the reference link i gave you
i hope this was helpful

Related

Convert Received Data from SQL and Translate

I am wanting to convert data that is queried from the database to another string. The best way I can explain what I'm trying to do is below:
I have a table called "users". Inside that table I have a column called "rank". Rank is a two-digit integer ranging from 1 to 21.
I want to convert that "1" or whichever number into more understandable text in my php file. So if your rank is set to "1", the output is "User" or what-have-you.
How do I go about doing this? I'm not sure what this process is called. I'm very new to php.
Thank you!
Create a rank table having 2 columns, id and rank:
id | rank
-------------
1 | user
2 | admin
3 | goof
Then JOIN the users table with the rank table:
SELECT u.name, r.rank
FROM users u
LEFT JOIN rank r
ON u.rank = r.id
You can store the ranks title in another table like mentioned by Jay Blanchard or you can define an array with the rank titles like this
$ranks = array(
1 => 'User',
2 => 'Administrator',
3 => 'Moderator'
);
and can echo like this
echo $ranks['rank_id_fetched_using_sql'];
rank_id_fetched_using_sql is supposed to be 1 to 21

Get All Records with same IDs and initial IDs (second column) In MySQL

On one of my project, I need to retrieve the latest version of a record from a table where the id maybe match with a second table called initial_id. Like this,
id 1 AND initial_id NULL // (same 1)
id 2 AND initial_id 1 // (same 1)
id 3 AND initial_id 1 // (same 1)
id 4 AND initial_id 2 // (same1)
id 5 AND initial_id 4 // (same 1)
id 6 AND initial_id NULL // (same 2)
id 7 AND initial_id NULL // (same 3)
id 8 AND initial_id 7 // (same 3)
In this case, my SELECT (with some subquerys) should retrieve those records because they are the latest version on each of them:
id 5 AND initial_id 4
id 6 AND initial_id NULL
id 8 AND initial_id 7
It is pretty easy to get the first three (id 1, id 2 AND id 3) because the initial_id are based on the first id but the other ones are harder...
Hope I'm clear enough.
Please help me out. Thanks !
UPDATE
This is my query so far:
SELECT a.id from table_a a WHERE a.id = (SELECT aa.id from table_a aa WHERE aa.id = a.id OR aa.initial_id = a.id ORDER BY aa.id DESC LIMIT 1)

Count likes of the latest date per user

I have a table "article_likes" of users and whether they like or dislike the article. With every like/dislike the datetime is saved and the id of the article. The like/dislike is saved as a bool in the "status" column.
id article_id user_id like_date status
1 8 2 2014-11-03 21:30:33 1
2 8 2 2014-11-03 21:31:00 0
3 8 3 2014-11-03 22:30:59 1
4 9 6 2014-11-03 22:36:25 1
5 9 2 2014-11-03 23:19:46 1
I like to get the amount of likes an article has. So only the status of the most recent date is valid for the count.
So the desired result would be:
article_id amount_likes
8 1
9 2
I want to get the result per article id, so not all at once.
Can anybody help me? Thanks!
This will solve your problem :)
SELECT article_id, SUM(IF(status = 1, 1, -1)) AS amount_likes FROM article_likes
GROUP BY article_id
If you store dislikes as -1 then:
SELECT article_id, SUM(status) AS amount_likes FROM article_likes
GROUP BY article_id
Try this:
SELECT `article_id`,COUNT(`user_id`) AS amount_likes FROM `links`
WHERE `status`='1' GROUP BY DATE(`like_date`), `user_id`;
Fiddle here - http://sqlfiddle.com/#!2/3b7bb6/4
Hope it helps
SELECT article_id, COUNT(IF(
SELECT CASE
WHEN status=1 THEN 'true'
ELSE 'false'
END),
'true','false'
)
)
FROM article_likes
GROUP BY article_id
ORDER BY article_id

JOIN with 2 Tables (Voting) Mysql, PHP

Hello I have 3 Tables but i need only help with 2 Tables.
The first Table is champions. In this Table are over 100 names.
The second Table is champion_names there are nicknames for the first Table.
The third Table is champion_names_vote. There are the votes for the nicknames. Thumb up if I like any nickname or thumb down.
On my website I have a site where I can see a list full of Names (Table 1). There are 2 columns. In the first is the normal name (Table 1) in the second the nickname (Table 2). Now I want to show the best nickname in column 2. Actually it's random but I only want to show the best nickname.
I can show all Names that's not the problem. But if I want to show only the best nicknames I don't know how.
Table 2: id(AI), champ_id(this is the id for Table 1), sender_id, name
Table 3: id(AI), userid, name_id(Table 2 ID), like_dislike
like_dislike = 1 is like, -1 is dislike and 0 is nothing.
Example:
Table 2: 50, 2, 4, Test
Table 3: 1, 3, 50, 1,
I liked the name of Table 2. So the name_id in Table 3 is the id of Table 2
So how can I do this with JOIN?
Can you help me please.
something like this should do the trick.
Idea is to Analyse your votes per nickname first. Then select the Maximum vote-score by HAVING:
SELECT voteResult.name, vote as winnerVoteScore
FROM(
select a.name_id, b.name,
sum(IF(a.like_dislike = 1, 1, IF(a.like_dislike = 2, -1, 0 ))) as vote
FROM champion_names_vote as a JOIN champion_names as b
ON a.name_id = b.ID
GROUP BY 1
) as voteResult
GROUP BY 1
HAVING vote = max(vote)

Getting advanced data from MySQL

I got a table named 'objects, events & events_types', and these tables got a structure (not the all structure of it, but the basic information which I need to display from the database) and some information like this:
Objects:
structure:
id(INT), title(TEXT)...
data:
1, House #1
2, House #2
3, House #3
Events:
structure:
id(INT), object_id(INT), event_type(INT)
data:
1, 1, 3
2, 1, 1
3, 2, 4
4, 2, 5
5, 2, 1
6, 1, 1
7, 2, 2
8, 1, 3
Events_Types:
structure: id(INT), name(VARCHAR)
data:
1, Alarm
2, Dis/Arm
3, Fire
4, Alarm button
5, Unknown
I have a page, which loads all data from events table, but I need the page displays me only those objects from objects table which has a data into events table. It won't load me id(3) object from objects table because there is no row into events table which has object_id 3.
I need a html table with the following columns:
objects.id
objects.title
events_types.name
count from events table where object_id of this table is the id of objects table and event_type from events table is got by $_GET['event_type'] from URL which will be defined as variable
Example of the table with information from database where $_GET['event_type'] = 1 and this '1' from events_types table is Alarm:
| 1 | House #1 | Alarm: 2 |
| 2 | House #2 | Alarm: 1 |
but the House #3 won't be displayed because there is no data for it in the events table.
For the information, in the real table I've got around 200 records in the objects table and around 500 records in the events table every month. It's like a statistic system for me.
From what you're describing, this sounds like a pretty straightforward join:
SELECT
objects.id,
objects.title,
events_types.name
FROM
objects INNER JOIN events on events.object_id = objects.id
INNER JOIN events_types on events_types.id = events.event_type;
This should give you one record for each event, which will be labeled with the name of that event type. Any objects that don't have events associated with them won't show up.
See if this query gets you the data you want:
SELECT O.id AS object_id, O.title, T.name, COUNT(*) as event_count
FROM Objects O
JOIN Events E ON O.id = E.object_id
JOIN Events_Types T ON E.id = T.id
GROUP BY O.id, E.event_type

Categories