Is it possible to combine 4 tables? - php

I need to query data from 4 tables. I'm not sure this is the best db design to approach this. This is simplified version. The actual table have more column.I have table for users who upload their song:
Id | Username |
---------------
1 | John |
2 | Michael |
3 | Frank |
Then I have song table like this
Id | Title | UserId
----------------------
1 | Title 1 | 1
2 | Title 2 | 1
3 | Title 3 | 2
4 | Title 4 | 2
Then a playlist table like this
Id | Title | UserId
------------------------
1 | My Playlist | 3
Then a playlist entries where a user save song id belong to a playlist
Id | SongId | PlaylistId
--------------------------
1 | 2 | 1
2 | 4 | 1
3 | 3 | 1
What I want to achieve after query is something like this:
Playlist | By | Song List
------------------------------------------------------------
My Playlist | Frank | Title 2 - John, Title 3 - Michael,
| | Title 4 - Michael
-------------------------------------------------------------
Any idea how to query this in MySQL. Or any better table design to achieve same result?

Try this:
SELECT
p.Title as Playlist,
u1.Username as By,
group_concat(s.Title, ' - ', u2.Username order by s.Id) as `Song List`
FROM playlist p
JOIN playlist_entries pe
ON p.Id= pe.PlaylistId
JOIN song s
ON pe.SongId = s.Id
JOIN user u1
ON p.UserId = u1.Id
JOIN user u2
ON s.UserId = u2.Id
Demo Here

Related

How to select values from two different tables in SQL

I have two tables in my SQL Server database. The first is catgeories and second is products. There is a column categories_id in both tables.
HERE IS MY TABLE FOR CATEGORIES :
+----+----------+----------+
| id | category | parent |
+----+----------+----------+
| 1 | BOY | 0 |
| 2 | GIRL | 0 |
| 3 | SHIRT | 1 |
| 4 | SKIRT | 2 |
| 5 | JACKET | 1 |
+----+----------+----------+
TABLE : PRODUCTS
+-------+--------------+----------------------+
| id | title |PRICE | Categories |
+-------+--------------+------+---------------+
| 1 | RED SHIRT | 300 | 3 |
| 2 | blue SKIRT | 500 | 4 |
| 3 | jeans jacket | 500 | 3 |
+-------+--------------+------+-----+---------+
Now I want to select the values from Products table for a particular category like BOY.
Try This:
SELECT pr.id,pr.title,pr.price from products AS pr
INNER JOIN CATEGORIES AS cat ON cat.id=pr.Categories
WHERE cat.category='Boy';
SELECT products.* FROM products INNER JOIN categories ON products.categories = categories.id WHERE categories.category LIKE '%BOY%';
Use this query..
Either
SELECT * FROM tproducts WHERE categories = 1
or
SELECT * FROM tproducts
JOIN tcategories ON tcategories.id = tproducts.categories WHERE tcategories.category = 'BOY'
I don't know your table names so I just used tproducts and tcategories
There is no boy(1) ID in Categories(field) in PRODUCTS(table)
SELECT * FROM CATEGORIES
INNER JOIN PRODUCTS
ON CATEGORIES.id=PRODUCTS.Categories
WHERE CATEGORIES.category ='BOY'
FOR this use join query Example:
select *(or you can get any column as you write name) from table1 join table2 on table1.id=table2.id where table1.category='BOY';

Mysql query string for photo sharing social app like Instagram

I am developing a photo sharing app platform. The app allows you to post a photo and others can like or rate the photo. Users can follow each other and see photos their 'followings' are sharing, just like instagram.
#user_tbl
id | name | number
-------------------
1 | Dan | 0209
2 | Sam | 2854
3 | Dave | 8123
4 | Alex | 5600
#photo_tbl
id | userid | path
-------------------
1 | 3 | dave-dog.jpg
2 | 1 | dans-cat.png
3 | 4 | alex-bird.jpg
4 | 2 | sam-fish.jpg
#friendship_tbl
id | actor | target
--------------------
1 | 2 | 1 // Sam is following Sam
2 | 2 | 4 // Sam is following Alex
3 | 1 | 3 // Dan is following Dave
4 | 4 | 2 // Alex is following Sam
#activities_stream_tbl
id | photoid | userid | context | date
----------------------------------------------------------
1 | 3 | 4 | add-new-photo | 10/10/2015
2 | 1 | 3 | add-new-photo | 12/10/2015
3 | 3 | 2 | Sam-share-Alex-photo | 15/10/2015
4 | 4 | 2 | add-new-photo | 20/10/2015
6 | 1 | 1 | Dan-like-Dave-photo | 21/10/2015
The #user_table holds the basic info of a user, while #photo_tbl hold the name and path of the photo shared by the user. In the #friendship_tbl is the relationship link between users. "actor" column is the id of the user doing the following while "target" column is the id of the user being followed.
I am currently having problem writing a query string to pull photos of USERX and photos of other users USERX is following and GROUP them by "photoid" in the activities_stream_tbl and ORDER BY "date" activities_stream_tbl.
I will be glad if anyone can help me, show me a better way of structuring db thank you.
to pull photos of USERX, you can construct your sql like
select PATH
from user_tbl as a inner join photo_tbl as b
on a.id = b.user_id
and a.name = 'userx'
and to pull photos of other users USERX is following, you may write
select path
from photo_tbl as a
where a.userid in (select target from friendship_tbl as x inner join user_tbl as y on x.actor = y.id and y.name = 'user')
you can union the above two results if you want.
ex:
select PATH
from user_tbl as a inner join photo_tbl as b
on a.id = b.user_id
and a.name = 'userx'
UNION
select path
from photo_tbl as a
where a.userid in (select target
from friendship_tbl as x
inner join user_tbl as y
on x.actor = y.id and y.name = 'user')

SQL: get data spread over 3 tables

I am trying to get some statistics for an online game I maintain. I am searching for an SQL statement to get the result on the bottom.
There are three tables:
A table with teams, each having a unique identifier.
table teams
---------------------
| teamid | teamname |
|--------|----------|
| 1 | team_a |
| 2 | team_x |
---------------------
A table with players, each having a unique identifier and optionally an affiliation to one team by it's unique teamid.
table players
--------------------------------
| playerid | teamid | username |
|----------|--------|----------|
| 1 | 1 | user_a |
| 2 | | user_b |
| 3 | 2 | user_c |
| 4 | 2 | user_d |
| 5 | 1 | user_e |
--------------------------------
Finally a table with events. The event (duration in seconds) is related to one of the players through their playerid.
table events.
-----------------------
| playerid | duration |
|----------|----------|
| 1 | 2 |
| 2 | 5 |
| 3 | 3 |
| 4 | 8 |
| 5 | 12 |
| 3 | 4 |
-----------------------
I am trying to get a result where the durations of all team members is summed up.
result
--------------------------
| teamid | SUM(duration) |
|--------|---------------|
| 1 | 14 | (2+12)
| 2 | 15 | (3+8+4)
--------------------------
I tried several combinations of UNION, WHERE IN, JOIN and GROUP but could not get it right. I am using PostgreSQL and PHP. Can anyone help me?
Just use sum with group by:
select t.teamid, sum(e.duration)
from team t
join players p on t.teamid = p.teamid
join events e on p.playerid = e.playerid
group by t.teamid
If you need all teams to be returned even if they don't have events, then use an outer join instead.
Try this
SELECT teamid, Sum(duration),
AS LineItemAmount, AccountDescription
FROM teams
JOIN teams ON teams.teamid = players.teamid
JOIN events ON players.playersid = events.playersid
JOIN GLAccounts ON InvoiceLineItems.AccountNo = GLAccounts.AccountNo
GROUP BY teamid
http://www.w3computing.com/sqlserver/inner-joins-join-two-tables/

MySQL finding rows in one table with information from another

Currently, I have two MySQL tables.
First table stores relation between the friend and his picture.
TABLE 1
id | pic_id | friend_id
----------------------------
0 | 123 | 84589
1 | 290 | 11390
2 | 884 | 84589
TABLE 2
Second table stores more information about the pic...
id | pic_id | title | color | detail
----------------------------------------------
0 | 123 | hello | black | brush
1 | 124 | world | red | paint
2 | 884 | sample | green | star
I have my friend_id and need to grab all the pic_id from Table 1 and then use the pic_id to grab the columns from Table 2(title, color, detail)...
How would I do this in MySQL?
Thank you!
Simply join the two tables.
SELECT b.title, b.color, b.detail
FROM table1 a INNER JOIN table2 b
on a.pic_id = b.pic_id
WHERE friend_id = 84589

mysql group by table b, inner join table a for a random

here is my 2 tables, id for a inner join event. i want to do this thing:
in table b, there has 10 albums, i want random get out 4 albums. then each album select one record, the record is random position in the album.
so that i will get 4 records back (these 4 records with no duplicate id), then take these 4 records for a inner join query, to get the title from table a.
here just little records just for test. in the fact, i have 300,000 records in table a and 2,000,000 records in table b.
table a
+-----+-------+
| id | title |
+-----+-------+
| 1 | a1 |
+-----+-------+
| 2 | a2 |
+-----+-------+
| 3 | a3 |
+-----+-------+
| 4 | a4 |
+-----+-------+
| 5 | a5 |
+-----+-------+
| 6 | a6 |
+-----+-------+
table b
+-----+--------+
| id | album |
+-----+--------+
| 1 | album1 |
+-----+--------+
| 2 | album1 |
+-----+--------+
| 3 | album1 |
+-----+--------+
| 6 | album1 |
+-----+--------+
| 2 | album2 |
+-----+--------+
| 3 | album2 |
+-----+--------+
| 5 | album3 |
+-----+--------+
| 6 | album3 |
+-----+--------+
| 3 | album4 |
+-----+--------+
| 2 | album5 |
+-----+--------+
| 4 | album5 |
+-----+--------+
| 5 | album5 |
+-----+--------+
| 1 | album6 |
+-----+--------+
| 3 | album6 |
+-----+--------+
| 2 | album7 |
+-----+--------+
| 4 | album7 |
+-----+--------+
| 1 | album8 |
+-----+--------+
| 5 | album8 |
+-----+--------+
| 3 | album9 |
+-----+--------+
| 2 | album10|
+-----+--------+
| 5 | album10|
+-----+--------+
I am not good at mysql query. In my mind I would do
select * from b group by album order by random() limit 0,4
get back 4 album, then do a inner join query (this query not correct, how to check the b.id no duplicate?)
select * from b inner join a on b.id = a.id where (select id from b where b.album = '".$row['album']."' order by random() limit 1)
I need an easy and quicker method, the best is just use one query. many thanks.
AFAIR, "ORDER BY RAND()" is extremely slow solutions, especially on tables like you have (2 million+ records), so I'd recommend looking at something similar to these kind of articles first: http://www.greggdev.com/web/articles.php?id=6
So, you should know the number of records in your table before running the query and then do something like:
"SELECT * FROM `album` LIMIT 1 OFFSET " . rand(0,$count)
This will return you 1 random row a bit more efficiently, I believe.
Also, I think it's not a good idea to store album references as string in tracks table, you'd rather use a proper integer foreign key album_id referenced to albums.id. Then you can join both tables much fatser. If I were you, I'd do first:
ALTER TABLE `tracks` add column `album_id` int;
UPDATE `tracks` SET `album_id` = SUBSTRING(`album`,5);
Then, after doing this and combining with the solution above, launch something like:
"SELECT * FROM `album` INNER JOIN `tracks`ON `tracks`.`album_id` = `albums`.`id` LIMIT 1 OFFSET " . rand(0,$count)
Since I'm neither an expert on MySQL nor on PHP, I'll try with pseudocode and generic SQL. I have renamed your tables to albums and tracks for sake of readability.
First fetch the four random records to your PHP application:
select id from albums order by random() limit 4
Second, iterate over the resulting result set of four IDs and fetch the corresponding tracks (pseudo-php):
foreach($album_ids as $id):
execute_query("select id from tracks where album_id = ? order by random(), limit 1", $id)
It is not obvious to me how you match your tracks to their albums. You should have something like tracks.album_id as a foreign key to albums.id, that's how I designed my queries. You should adapt as appropriate, the underlying logic behind my solution should remain the same.

Categories