This is my first post and after searching and even trying to use the chatbots I still cant progress.
I'm trying to array_append or array_cat one text array called 'genres' in table artists, to an another 'genres' text array in the table 'event_data'. This is itself is not difficult, but what im trying to do is use the rows in the artists table, each with a genre array, and append the array to the row where the 'artist_name' in artists is found in another text array call called 'artists' in event data.
Please note that the order of the artist table is not the same as the events table so although you cant see the artist_name in the artists column in the snippet, its there, I just limited to 4 to keep it clean. Somewhat unsuccessfully
event_name artists genres
manic.monday ["SUNRISE_GROOVE"] ["None"]
Traffic light Party ["digital"] ["Techno","Reggaeton"]
Xterrestrial Launch Party ["CNVNHausbr and xenophil"] ["Techno","Tech House"]
THE UNKNOWN ["Better Call Paul","Dicso_Muzek"] ["None"]
artist_name genres
Dicso_Muzek ["None","Deep","(Deep,Electronic,Techno,House)"]
TiM TASTE ["Progressive House","Tech House""]
Hausbrand ["Techno","Tech House","(\"Hard Techno\""]
DAV3 ["Techno","Tech House","(Techno,House)"]
Thanks in advance for any help and teachings you can provide
I've tried these queries and more but i'm having issues with the sub-query returning more than one row,which it needs to. There maybe 10 artists in the event_data.artists array and a lot of artist_name rows with their genre arrays in the artist table.
UPDATE event_data SET genres = array_append(genres,
(SELECT genres FROM artists WHERE artist_name = ANY(
SELECT UNNEST(artists) FROM event_data)))::text;
UPDATE event_data
SET genres = array_cat(genres, (
SELECT array_agg(genres)
FROM artists
WHERE artists.artist_name = ANY(event_data.artists)))
FROM event_data;*/
UPDATE event_data
SET genres = array_cat(genres, (
SELECT genres
FROM artists
WHERE artist_name = ANY (event_data.artists)
))
WHERE artist_name IN (SELECT artist_name FROM artists)
I'm expecting to have each row of event_data to have all the genres from each artist in the genre array.
Because the artists[] string is inputted in a separate data scrape from the collection of the artist names, I haven't appended them in the process.
So I managed to figure it out. I used a temp table to join the arrays found in the artists table before inserting them into the other array
WITH temp_table AS (
SELECT artist_name, unnest(artists.genres) AS genre
FROM event_data
JOIN artists ON artist_name = ANY (event_data.artists))
UPDATE event_data
SET genres = array_cat(event_data.genres::text[], (SELECT array_agg(genre) FROM temp_table))
Related
Using MYSQL, if i have a table SONG with the columns (idSONG, TITLE, LENGTH), a table ARTIST with the columns (idARTIST, SINGLES, BIOGRAPHY), with idARTIST being a fk for ID in a table PEOPLE with columns (ID, NAME), a relation RELEASES (idSONG, idARTIST). How can i make a procedure so that i input an Artists name and it selects all the Songs by that Artist? I know the tables arent really the most efficient but its for a school project.
I don't think we need anything from the ARTIST table, so I think that given an artists's name we can join PEOPLE, RELEASES, and SONG to achieve the desired result as follows:
SELECT TITLE
FROM SONG S
JOIN RELEASES R ON S.idSONG = R.idSONG
JOIN PEOPLE P ON R.idARTIST = P.ID
WHERE P.NAME = 'insert artist name here';
I have a MySQL DB storing song metadata. Two of the existing columns in Table Music are Rating and Title. The DB is over 9 GB large. I would like to find all unique song titles where the rating is below a value on a 0-10 scale, say 3. (Unique here meaning I only need to see a song title once.)
Once I get the resulting list of song titles, I will delete those low-rated songs form the DB. However, I have another table in the DB called Albums and I never want to delete any songs in any Album listed there.
What I have tried so far is:
SELECT DISTINCT Title FROM Music WHERE
Title NOT IN (SELECT Title FROM Music WHERE Rating >= 3)
AND Song NOT IN (SELECT Song FROM Albums)
ORDER BY Title desc
This query might work; I don't know because it has been running for hours. So I also need help in finding a faster way to get the results.
Get songs from MUSIC with rating below 3 then do a left join with ALBUM on key: song. But also put a filter where song in MUSIC is not found in album.
SELECT DISTINCT T.TITLE
FROM MUSIC T
LEFT JOIN ALBUM A ON T.SONG = A.SONG
WHERE T.RATING < 3
AND A.SONG IS NULL
ORDER BY T.TITLE DESC;
see demo here;
http://sqlfiddle.com/#!9/65c2455/1
Select maintable.name FROM maintable
JOIN genres genre1 USING (tmdb_id)
JOIN genres genre2 USING (tmdb_id)
WHERE genre1.genres_name = 'Action'
AND genre2.genres_name = 'Drama'
group by maintable.name
Here genres is table name. genres_name is column name. genres1 and genres2 are just nor a table name, nor a column name, they are just random name in the code.
This is my code, now How do i display all genres_name?
The genres is like:
tmdb_id genres_name
1 Action
1 Crime
1 Drama
2 Horror
2 Comedy
2 Drama
The main table isl ike
tmdb_id movie_title
1 The Dark Knight
2 Logan
3 Wonder Woman
Let me know, if you need more information. (Please do not ask to show, what i tried. Trust me, it will make the question more confusing)
I want to echo the genres like:
The Dark Knight - Drama, Action, Crime
Of course, you need to use group_concat:
Select maintable.movie_title, group_concat(genres.genres_name) AS genres_name
FROM maintable
JOIN genres USING (tmdb_id)
GROUP BY maintable.tmdb_id
HAVING find_in_set('Action', genres_name) AND find_in_set('Drama', genres_name)
See demo here.
Note: How does find_in_set works, please see official doc.
I would try something like this. But this is the best I can do guessing at it in my head... ( sorry for any mistakes )
$Sql = "SELECT
m.name,
GROUP_CONCAT( g.genres_name ) as genres_list
FROM
maintable AS m
JOIN
genres AS g USING (tmdb_id)
WHERE
g.genres_name IN('Drama', 'Action')
GROUP BY m.tmdb_id";
MySQL GROUP_CONCAT() function returns a string with concatenated non-NULL value from a group.
http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php
Also note GROUP_CONCAT has a setting for the length, I don't recall what that is or how to change it, but it bit me in the butt one time. Basically it will truncate the list after a certain size, so be cautious of that.
See here: MySQL and GROUP_CONCAT() maximum length
AS I said I haven't tested this, but it seems you have a many to one relationship. Records in the maintable can have many related records in the genres table. Therefor, you should be able to group them on that relationship. Normally this would return 1 record for each pair ( same record in main table different in genre ) Without the group. The Group Concat allows you to compress that into a comma separated list.
I've created some tables in MySQL and I'm trying to select the data that's in those tables so that they get displayed. I have the following tables. Formed, Track, Album, Band, Customers and I have got the primary and foreign keys within the tables. I've also used the Insert into statement to insert data into the tables so there is data within the tables. I'm trying to get all the albums from a certain band to be displayed and I'm following the example below
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
Which can be found at W3Schools, to produce the following statement:
SELECT Album.AlbumID, Album.Title, Band.BandName
FROM Album
INNER JOIN Band
ON Album.BandID = Band.BandID
ORDER BY Band.BandName;
And this statement that I've made does create the table headers but doesn't display any data as shown here.
EDIT
In all the tables I've created, I hav got the primary key as an int AUTO_INCREMENT
EDIT #2
I've now changed it so that I have:
SELECT Album.AlbumID, Album.Title, Band.BandName
FROM Band
LEFT JOIN Album
ON Album.BandID = Band.BandID
ORDER BY Band.BandName;
Which displays only the band name and gets a NULL values for the AlbumID and Title
EDIT #3 Completed
I've managed to edit it so now I have the script working. It's as follows:
SELECT Album.AlbumID, Album.Title, Band.BandName
FROM Album
LEFT JOIN Band
ON Album.BandName = Band.BandName
ORDER BY Band.BandName;
I want to show records from table name station where station have at least one song in song table.
Table structure
station
station_id
stration_name
station_description
song
song_id
station_id
song_location
Please suggest me the way to form query that shows me station data which have songs in song table.please specify a way that do not returns record with corresponding songs zero count.
What you're looking for is a INNER JOIN. You could join your stations table with your songs table by stations.station_id and songs.station_id. This will work because INNER JOIN only return rows for which the join-predicate is satisfied.
I've made an example available at SQL Fiddle, but I do recommend spending a few minutes understanding mechanics of JOIN.
SELECT DISTINCT something
FROM somewhere
JOIN somewhere_else
ON somewhere_else.other_thing = somewhere.thing;
You could join the tables together at station_id.
It looks like each song is linked to a spesific station. Meaning where these ID (station_id) is equal, the station has this song...