Data not being displayed when selecting two columns MySQL - mysql

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;

Related

MYSQL procedure to select data with input from other tables

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';

SQL Query Including Joins

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

Mysql - Getting duplicate results

I have 3 tables UPDATES, MODELS, and UPDATES_MODELS.
The UPDATES table has the following columns:
update_id,
posted
thumbnail
The MODELS table has the following columns:
model_id
first_name
last_name.
The UPDATES_MODELS is a linking table and has the following columns:
update_id
model_id.
Some updates have more than 1 model. For example if an update has 2 models, my query will return 2 duplicate thumbnails, 2 duplicate titles,
2 duplicate dates, and 1 update will have 1 model name and the other update will have the other model name.
This is not what I want.
An update with 2 or more models should return 1 thumbnail, 1 title, 1 date and list both model names with the update.
I tried using DISTINCT in SQL query, that did nothing. I got the same result with or without the DISTINCT keyword.
Here's is my code:
SELECT updates.update_id, title, posted, duration, updates.thumbnail, updates.alt_tag, updates.title_tag, updates.visible, models.model_id, first_name, last_name, updates_models.update_id, updates_models.model_id
FROM updates
INNER JOIN updates_models
ON updates.update_id = updates_models.update_id
INNER JOIN models
ON models.model_id = updates_models.model_id
First of all I doubt you table model. If you have the case 'Update have multiple Models, but not same Model can own to multiple Update', then it's one to many relation ship and you can just keep update_id in Models table.
Your question is not very clear and incomplete. Your query has columns which you have not explained in the question. However you can try below query for your case if I got you properly with limited details.
SELECT updates.update_id, posted, updates.thumbnail,array_agg(first_name), array_agg(last_name), updates_models.update_id, updates_models.model_id FROM "UPDATES" updates INNER JOIN "UPDATES_MODELS" updates_models ON updates.update_id = updates_models.update_id INNER JOIN "MODELS" models ON models.model_id = updates_models.model_id group by updates.update_id, posted, updates.thumbnail, updates_models.update_id, updates_models.model_id

MYSQL one to many: acessing the primary key from the fitst table, not the Fk from the second

A mysql one to many question: various products and where I sell them. I have a variety of products that I can sell, and several locations to sell them. I have a products table with a Primary Key: product_id. I have a locations table with PK location_id and a field product_id to link to the products table.
So, SELECT * FROM products LEFT JOIN locations on product_id.
I got an ambiguous column error. That doesn't make sense, they should line up.
So: SELECT * FROM products LEFT JOIN locations on products.product_id = locations.product_id.
That worked.
Here's the problem: there are many products I can get but don't currently sell. Some products have no locations. To create a link to add a location, I need the product_id. If I print out $row['product_id'] I am getting the product ids from the locations table, which is blank if they are unsold. I need the product ids from the first table.
This can't be an unusual situation.
Ideas?
You need to specify in your SELECT statement that you want to use the product_id from the products table. Here is a sample of what I am talking about (SQL Fiddle):
SELECT p.product_id, p.description, p.unit_of_measure, l.location
FROM products p
LEFT JOIN locations l on l.product_id = p.product_id
As you can see for product_id 3 there is no location but yet I am still displaying the product_id.
MySQL supports the using clause, so you can write the first version as:
SELECT *
FROM products LEFT JOIN
locations
USING (product_id);
Without the using clause, MySQL needs to know the table where a column comes from. Because there are two tables with the same column name, MySQL is confused.
This also has the advantage that the * only includes the product_id column once, not twice.

MySQL view - many to one relationship in one row

I have a table that holds the movie information. It has ID, title and so on. I have another table called categories where I have the available categories; action, drama and so on.
Each movie can be in many categories. So I created a view and joined these tables. Now the view displays a row for each category even if the movie is repeated.
I need to have have a single row for each movie and have categories as something like : 'Action, Drama, Comedy' (which is basically all categories from tbl_movies_categories).
How should I join tables/create view to achieve this?
This is the proper way:
Group By a unique ID, then use group_concat() to concat the values together.
CREATE VIEW `vw_metadata` AS
select
`vw_movies`.`id` AS `id`,
...
group_concat(`vw_movies_categories`.`title_category`
separator ',') AS `categories`,
from
(`vw_movies`
left join `vw_movies_categories` ON (`vw_movies_categories`.`movie_id` = `vw_movies`.`id`))
group by `vw_movies`.`id`