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';
Related
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))
So I have a database with two tables, profile, and friends. The primary key in profile is an auto incremented int, and it is a foreign key in friends. The fields in the profile table are: id, name, age, bio, motto, email_address.
The fields in friends is: initiator_id, receiver_id, date_added.
So ultimately I am trying to make a query where I set the initiator_id and get a list of receiver_id's, and use those id numbers to get them from the profile table.
I've tried left join's, inner joins, and joins in general. Open to suggestions, and interpretations on what these types of joins are actually doing.
select friends.receiver_id, profile.name
from profile
inner join friends on friends.initiator_id=1;
I need the fields to return the receiver_id number as well as the corresponding name for that id number.
The specification is a bit unclear. Sample data and expected output would go a longs ways towards illustrating the requirements.
But my guess (and without a specification, its just a guess) is that we are after the resultset returned from this query:
SELECT f.receiver_id
, p.name
FROM friends f
JOIN profile p
ON p.id = f.receiver_id
WHERE f.initiator_id = 1
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 have two tables: teams, and persons.
table teams have three columns, id, name, leader
table persons have these columns: hash, team_id
teams.leader is a MD5 hash that must match persons.hash in order to determine which person is leader of a given team.
I need to run a query on MySQL that does the following:
1) Retrieve all the leaders of a team, and the team id:
SELECT `id`,`leader` FROM `teams`;
2) Use such information to update team_id on table persons
This is my current Query:
SELECT id FROM teams INNER JOIN persons ON teams.leader = persons.hash
but I haven't been able to come up with a solution that allows me update column team_id with the corresponding leader.
I've been thinking probably using cursors, but not sure about it.
Any ideas?
You can use the multiple table UPDATE syntax to join the tables:
UPDATE teams JOIN persons ON teams.leader = persons.hash
SET persons.team_id = teams.id
I have a database of articles, which are stored in categories. For my homepage, I want to grab an article from each category (I don't care which). However, some articles are crossposted to multiple categories, so they come up twice.
I have a table called tblReview with the article fields (reviewID, headline, reviewText) and a table called tblWebsiteContent that tells the site which categories the articles are in (id, reviewID, categoryID) and finally, a table called tblCategories (categoryID, categoryName) which stores the categories.
My query basically joins these tables and uses GROUP BY tblCategory.categoryID. If I try adding 'tblReview.reviewID' into the GROUP BY statement, I end up with hundreds of articles, rather than 22 (the number of categories I have).
I have a feeling this needs a subquery but my test efforts haven't worked (not sure which query needs to contain my joins / field list / where clause etc).
Thanks!
Matt
SELECT T.categoryName, tR.headline, tR.reviewText
FROM (
SELECT tC.categoryName, MAX(tR1.reviewID) reviewID
FROM tblReview tR1 join tblWebsiteContent tWC on tR1.reviewID = tWC.reviewID
join tblCategory tC on tC.categoryID = tWC.categoryID
GROUP BY tC.categoryName) T JOIN
tblReview.tR on tR.reviewID = T.reviewID
this query will select for each category an article headline corresponding to the Max reviewId for that category (you said 'I don't care which')
Try using SELECT DISTINCT. (This will only work if your SELECT is only pulling the article ID.)
select DISTINCT reviewID