How can I make view in mysql using JOIN? - mysql

There are two tables, table1 and table2.
[ table1 ]
+----+------+
| no | time |
+----+------+
| 1 | 1111 |
+----+------+
| 2 | 2222 |
+----+------+
| 3 | 3333 |
+----+------+
| 4 | 4444 |
+----+------+
| 5 | 5555 |
+----+------+
[ table2 ]
+----+-----+----------+------+
| no | idx | name | rank |
+----+-----+----------+------+
| 1 | 1 | Apple | 1 |
+----+-----+----------+------+
| 2 | 1 | Banana | 2 |
+----+-----+----------+------+
| 3 | 1 | Car | 3 |
+----+-----+----------+------+
| 4 | 1 | Dragon | 4 |
+----+-----+----------+------+
| 5 | 1 | Eagle | 5 |
+----+-----+----------+------+
| 6 | 2 | Fire | 2 |
+----+-----+----------+------+
| 7 | 2 | God | 3 |
+----+-----+----------+------+
| 8 | 2 | Hippo | 4 |
+----+-----+----------+------+
| 9 | 3 | Icecream | 1 |
+----+-----+----------+------+
| 10 | 3 | Juice | 3 |
+----+-----+----------+------+
| 11 | 3 | Korea | 4 |
+----+-----+----------+------+
| 12 | 3 | Low | 5 |
+----+-----+----------+------+
| 13 | 4 | Mother | 2 |
+----+-----+----------+------+
| 14 | 4 | News | 3 |
+----+-----+----------+------+
| 15 | 5 | Object | 1 |
+----+-----+----------+------+
And, I want to make a view like follow image using mysql.
+----+-----+----------+------+------+
| no | idx | name | time | rank |
+----+-----+----------+------+------+
| 1 | 1 | Apple | 1111 | 1 |
+----+-----+----------+------+------+
| 2 | 1 | Banana | 1111 | 2 |
+----+-----+----------+------+------+
| 3 | 1 | Car | 1111 | 3 |
+----+-----+----------+------+------+
| 4 | 1 | Dragon | 1111 | 4 |
+----+-----+----------+------+------+
| 5 | 1 | Eagle | 1111 | 5 |
+----+-----+----------+------+------+
| 1 | 2 | Apple | 2222 | 1 |
+----+-----+----------+------+------+
| 6 | 2 | Fire | 2222 | 2 |
+----+-----+----------+------+------+
| 7 | 2 | God | 2222 | 3 |
+----+-----+----------+------+------+
| 8 | 2 | Hippo | 2222 | 4 |
+----+-----+----------+------+------+
| 5 | 2 | Eagle | 2222 | 5 |
+----+-----+----------+------+------+
| 9 | 3 | Icecream | 3333 | 1 |
+----+-----+----------+------+------+
| 6 | 3 | Fire | 3333 | 2 |
+----+-----+----------+------+------+
| 10 | 3 | Juice | 3333 | 3 |
+----+-----+----------+------+------+
| 11 | 3 | Korea | 3333 | 4 |
+----+-----+----------+------+------+
| 12 | 3 | Low | 3333 | 5 |
+----+-----+----------+------+------+
| 9 | 4 | Icecream | 4444 | 1 |
+----+-----+----------+------+------+
| 13 | 4 | Mother | 4444 | 2 |
+----+-----+----------+------+------+
| 14 | 4 | NEws | 4444 | 3 |
+----+-----+----------+------+------+
| 11 | 4 | Korea | 4444 | 4 |
+----+-----+----------+------+------+
| 12 | 4 | Low | 4444 | 5 |
+----+-----+----------+------+------+
| 15 | 5 | Object | 5555 | 1 |
+----+-----+----------+------+------+
| 13 | 5 | Mother | 5555 | 2 |
+----+-----+----------+------+------+
| 14 | 5 | News | 5555 | 3 |
+----+-----+----------+------+------+
| 11 | 5 | Korea | 5555 | 4 |
+----+-----+----------+------+------+
| 12 | 5 | Low | 5555 | 5 |
+----+-----+----------+------+------+
The range of rank column is 1 to 5.
And, If there is no data like (idx2 & rank5), then I want to use the data which has same rank and biggest idx and lower idx than that data.
I try to use following query.
select * from (`table1` JOIN (select 1 AS `rank` union select 2 union select 3 union select 4 union select 5) AS `x` )
But, I don't know how should I do in the future.

try this
select ifnull(t2a.no, t2b.no) no, t.idx, ifnull(t2a.name, t2b.name) name, t.rank
from (
select t3.idx, t1.no rank, (select max(idx) from table2 where rank = t1.no and idx<t3.idx) max_idx
from (select distinct idx from table2 ) t3
cross join table1 t1
) t
left join table2 t2a on t2a.rank=t.rank and t.idx = t2a.idx
left join table2 t2b on t2b.rank=t.rank and t.max_idx = t2b.idx
order by 2,4

Related

Is there a mySQL procedure that can merge duplicate rows of data into one, then allow me to manipulate that data as if it were one row?

I'm trying to come up with a stored procedure that takes multiple rows that are exactly identical, and combines them into one row while summing one column, which can then be run through more stored procedures based on the sum of that one column.
I've tried a GROUP BY statement, but that doesn't actually group the rows together, because if I run the table through another procedure it performs actions as if each row were not combined. Performing a SELECT * FROM mytable query shows that each row was not actually combined into one.
Is there any way to permanently combine multiple rows into one singular row?
To start, I've got a table like this:
+-------+-----+--------+---------+------+-----+-----------+
| RowID | pID | Name | Date | Code | QTY | Purchased |
+-------+-----+--------+---------+------+-----+-----------+
| 1 | 1 | bob | 9/29/20 | 123 | 1 | |
| 2 | 1 | bob | 8/10/20 | 456 | 1 | |
| 3 | 2 | rob | 9/15/20 | 123 | 1 | |
| 4 | 2 | rob | 9/15/20 | 123 | 1 | |
| 5 | 2 | rob | 9/15/20 | 123 | 1 | |
| 6 | 2 | rob | 9/15/20 | 123 | 1 | |
| 7 | 2 | rob | 9/15/20 | 123 | 1 | |
| 8 | 3 | john | 7/12/20 | 987 | 1 | |
| 9 | 3 | john | 7/12/20 | 987 | 1 | |
| 10 | 4 | george | 9/12/20 | 684 | 1 | |
| 11 | 5 | paul | 2/2/20 | 454 | 1 | |
| 12 | 6 | amy | 1/12/20 | 252 | 1 | |
| 13 | 7 | susan | 5/30/20 | 131 | 1 | |
| 14 | 7 | susan | 6/6/20 | 252 | 1 | |
| 15 | 7 | susan | 5/30/20 | 131 | 1 | |
+-------+-----+--------+---------+------+-----+-----------+
By the end, i'd like to have a table like this:
+-------+-----+--------+---------+------+-----+-----------+
| RowID | pID | Name | Date | Code | QTY | Purchased |
+-------+-----+--------+---------+------+-----+-----------+
| 1 | 1 | bob | 9/29/20 | 123 | 1 | |
| 2 | 1 | bob | 8/10/20 | 456 | 1 | |
| 3 | 2 | rob | 9/15/20 | 123 | 5 | |
| 4 | 3 | john | 7/12/20 | 987 | 2 | |
| 5 | 4 | george | 9/12/20 | 684 | 1 | |
| 6 | 5 | paul | 2/2/20 | 454 | 1 | |
| 7 | 6 | amy | 1/12/20 | 252 | 1 | |
| 8 | 7 | susan | 5/30/20 | 131 | 2 | |
| 9 | 7 | susan | 6/6/20 | 252 | 1 | |
+-------+-----+--------+---------+------+-----+-----------+
Where exactly identical rows are combined into one row, and the QTY field is summed, that I can then add purchases to, or make deductions from the quantity as a total. Using GROUP BY statements can achieve this, but when I go to alter the quantity or add purchases to each person, it treats it like the first table, as if nothing was actually grouped.
So you have this table:
| RowID | pID | Name | Date | Code | QTY | Purchased |
+-------+-----+--------+---------+------+-----+-----------+
| 1 | 1 | bob | 9/29/20 | 123 | 1 | |
| 2 | 1 | bob | 8/10/20 | 456 | 1 | |
| 3 | 2 | rob | 9/15/20 | 123 | 1 | |
| 4 | 2 | rob | 9/15/20 | 123 | 1 | |
| 5 | 2 | rob | 9/15/20 | 123 | 1 | |
| 6 | 2 | rob | 9/15/20 | 123 | 1 | |
| 7 | 2 | rob | 9/15/20 | 123 | 1 | |
| 8 | 3 | john | 7/12/20 | 987 | 1 | |
| 9 | 3 | john | 7/12/20 | 987 | 1 | |
| 10 | 4 | george | 9/12/20 | 684 | 1 | |
| 11 | 5 | paul | 2/2/20 | 454 | 1 | |
| 12 | 6 | amy | 1/12/20 | 252 | 1 | |
| 13 | 7 | susan | 5/30/20 | 131 | 1 | |
| 14 | 7 | susan | 6/6/20 | 252 | 1 | |
| 15 | 7 | susan | 5/30/20 | 131 | 1 | |
The best way, as has been suggested, is to create a new table with the content of your query, then to rename the old table, and the new table to the original table's name, to check if everything is all right, and to drop the original table if yes.
CREATE TABLE indata_new AS
WITH grp AS (
SELECT
MIN(rowid) AS orowid
, pid
, name
, MAX(date) AS date
, code
, SUM(qty) AS qty
FROM indata
GROUP BY
pid
, name
, code
)
SELECT
ROW_NUMBER() OVER(ORDER BY orowid ASC) AS rowid
, *
FROM grp;
ALTER TABLE indata RENAME TO indata_old;
ALTER TABLE indata_new RENAME TO indata;
-- if "indata" now contains the data you want ...
SELECT * FROM indata;
-- out rowid | orowid | pid | name | date | code | qty
-- out -------+--------+-----+--------+------------+------+-----
-- out 1 | 1 | 1 | bob | 2020-09-29 | 123 | 1
-- out 2 | 2 | 1 | bob | 2020-08-10 | 456 | 1
-- out 3 | 3 | 2 | rob | 2020-09-15 | 123 | 5
-- out 4 | 8 | 3 | john | 2020-07-12 | 987 | 2
-- out 5 | 10 | 4 | george | 2020-09-12 | 684 | 1
-- out 6 | 11 | 5 | paul | 2020-02-02 | 454 | 1
-- out 7 | 12 | 6 | amy | 2020-01-12 | 252 | 1
-- out 8 | 13 | 7 | susan | 2020-05-30 | 131 | 2
-- out 9 | 14 | 7 | susan | 2020-06-06 | 252 | 1
-- you can ...
DROP TABLE indata_old;

MySQL Query to display ingredients

I have the following tables
mysql> select * from drink;
+----+--------------+----------+---------+
| id | name | location | alcohol |
+----+--------------+----------+---------+
| 1 | Ginger Ale | 13 | 0 |
| 2 | Whiskey | 1 | 1 |
| 3 | Vodka | 2 | 1 |
| 4 | Rum | 3 | 1 |
| 5 | Gin | 4 | 1 |
| 6 | Tequila | 5 | 1 |
| 7 | Triple Sec | 6 | 1 |
| 8 | Cola | 14 | 0 |
| 10 | Cherry Vodka | 7 | 1 |
| 11 | Sprite | 15 | 0 |
| 12 | Sour Mix | 0 | 0 |
| 13 | Lemon Juice | 0 | 0 |
| 14 | Lime Juice | 0 | 0 |
| 15 | Grenadine | 0 | 0 |
| 16 | Orange Juice | 16 | 0 |
+----+--------------+----------+---------+
15 rows in set (0.00 sec)
mysql> select * from cocktail;
+----+-------+----------------------------------+
| id | name | comment |
+----+-------+----------------------------------+
| 1 | Item1 | Whiskey Ginger |
| 2 | Item2 | Screwdriver |
| 3 | Item3 | Cherry Vodka and Sprite |
| 4 | Item4 | Bartender's Long Island Iced Tea |
| 5 | Item5 | Long Island Iced Tea |
| 6 | Item6 | Gin and Sin |
+----+-------+----------------------------------+
6 rows in set (0.00 sec)
mysql> select * from mix;
+----+---------+------------+--------+
| id | drinkID | cocktailID | ounces |
+----+---------+------------+--------+
| 1 | 1 | 1 | 4.00 |
| 2 | 2 | 1 | 1.00 |
| 9 | 10 | 3 | 1.00 |
| 10 | 11 | 3 | 4.00 |
| 11 | 5 | 4 | 0.75 |
| 12 | 4 | 4 | 0.75 |
| 13 | 6 | 4 | 0.75 |
| 14 | 3 | 4 | 0.75 |
| 15 | 7 | 4 | 1.00 |
| 16 | 12 | 4 | 1.00 |
| 17 | 8 | 4 | 1.00 |
| 18 | 3 | 5 | 0.75 |
| 19 | 5 | 5 | 0.75 |
| 20 | 4 | 5 | 0.75 |
| 21 | 6 | 5 | 0.75 |
| 22 | 12 | 5 | 2.00 |
| 23 | 8 | 5 | 1.00 |
| 24 | 7 | 5 | 1.00 |
| 25 | 5 | 6 | 2.00 |
| 26 | 13 | 6 | 2.00 |
| 27 | 15 | 6 | 2.00 |
| 28 | 16 | 6 | 2.00 |
| 33 | 3 | 2 | 1.50 |
| 34 | 16 | 2 | 4.00 |
+----+---------+------------+--------+
24 rows in set (0.00 sec)
From these tables I want to create a joined table that has the ID, Comment, Ingredient Name, and Number of ounces.
So far I have this query but I keep getting errors and I am not sure where I am going wrong.
select cocktail.comment
, drink.name
, mix.ounces
from cocktail
join mix
on mix.drinkID = drink.ID
join cocktail
on mix.cocktailID = cocktail.id;
I think that there is something wrong with my join statement but the MySQL error is ambiguous to me
ERROR 1066 (42000): Not unique table/alias: 'cocktail'
Would someone be able to provide me some guidance? Thank you!
You're query is calling the cocktail table twice. If you want to do that you need to use an alias to give unique names to the calls, but it looks like you're missing the JOIN to the drink table. It seems like you want:
select
c.comment,
d.name,
m.ounces
from drink d
inner join mix m
on m.drinkID = d.ID
inner join cocktail c
on m.cocktailID = c.id;
Here is a demo. This difference with this version and your version is it includes a call to the drink table to get the name of the ingredient.

How do I combine two nested MySQL queries into one View?

I have two queries, almost similar, but never the less,They must be treated as separate as they have different meanings and values, I want to combine them into one view, I tied doing UNION, but the result was they were all combined into one table, which is not what I want, I would like them to appear as entirely separate tables under one view, here is what I did:
CREATE VIEW TEAM_SUMMARY AS
SELECT DISTINCT COUNTRY.country_name AS CountryName_T1,count(Team1)AS NoOfGames,
SUM(Team1_score) AS TotalGoalsFor,SUM(Team2_score) AS TotalGoalsAgainst
FROM COUNTRY,MATCH_RESULTS WHERE
country_name = Team1
group by country_name
UNION
SELECT DISTINCT COUNTRY.country_name AS CountryNameT_2,count(Team2)AS NoOfGames,
SUM(Team2_score) AS TotalGoalsFor,SUM(Team1_score) AS TotalGoalsAgainst
FROM COUNTRY,MATCH_RESULTS WHERE
country_name = Team2
group by country_name;
UPDATE:
So, the output of my current query is something like this:
mysql> SELECT * FROM TEAM_SUMMARY;
+----------------------+-----------+---------------+-------------------+
| CountryName | NoOfGames | TotalGoalsFor | TotalGoalsAgainst |
+----------------------+-----------+---------------+-------------------+
| Algeria | 1 | 1 | 1 |
| Argentina | 4 | 5 | 1 |
| Australia | 2 | 2 | 6 |
| Belgium | 3 | 5 | 2 |
| Bosnia & Herzegovina | 1 | 3 | 1 |
| Brazil | 6 | 7 | 13 |
| Cameroon | 2 | 1 | 8 |
| Chile | 1 | 3 | 1 |
| Columbia | 3 | 7 | 1 |
| Costa Rica | 2 | 1 | 1 |
| Croatia | 1 | 1 | 3 |
| Ecuador | 1 | 0 | 0 |
| England | 1 | 1 | 2 |
| France | 3 | 5 | 1 |
| Germany | 4 | 9 | 3 |
| Ghana | 1 | 1 | 2 |
| Greece | 1 | 2 | 1 |
| Honduras | 2 | 1 | 5 |
| Iran | 1 | 0 | 0 |
| Italy | 2 | 0 | 2 |
| Ivory Coast | 1 | 2 | 1 |
| Japan | 2 | 1 | 4 |
| Mexico | 1 | 1 | 0 |
| Netherlands | 4 | 4 | 1 |
| Nigeria | 2 | 3 | 3 |
| Portugal | 1 | 2 | 1 |
| Russia | 1 | 1 | 1 |
| South Korea | 2 | 2 | 5 |
| Spain | 2 | 1 | 7 |
| Switzerland | 2 | 4 | 6 |
| Uruguay | 2 | 3 | 4 |
| USA | 2 | 2 | 3 |
| Algeria | 3 | 6 | 6 |
| Argentina | 3 | 3 | 3 |
| Australia | 1 | 1 | 3 |
| Belgium | 2 | 1 | 1 |
| Bosnia & Herzegovina | 2 | 1 | 3 |
| Brazil | 1 | 4 | 1 |
| Cameroon | 1 | 0 | 1 |
| Chile | 3 | 3 | 3 |
| Columbia | 2 | 5 | 3 |
| Costa Rica | 3 | 4 | 1 |
| Croatia | 2 | 5 | 3 |
| Ecuador | 2 | 3 | 3 |
| England | 2 | 1 | 2 |
| France | 2 | 5 | 2 |
| Germany | 3 | 9 | 1 |
| Ghana | 2 | 3 | 4 |
| Greece | 3 | 1 | 4 |
| Honduras | 1 | 0 | 3 |
| Iran | 2 | 1 | 4 |
| Italy | 1 | 2 | 1 |
| Ivory Coast | 2 | 2 | 4 |
| Japan | 1 | 1 | 2 |
| Mexico | 3 | 4 | 3 |
| Netherlands | 3 | 11 | 3 |
| Nigeria | 2 | 0 | 2 |
| Portugal | 2 | 2 | 6 |
| Russia | 2 | 1 | 2 |
| South Korea | 1 | 1 | 1 |
| Spain | 1 | 3 | 0 |
| Switzerland | 2 | 3 | 1 |
| Uruguay | 2 | 1 | 2 |
| USA | 2 | 3 | 3 |
+----------------------+-----------+---------------+-------------------+
64 rows in set (0.01 sec)
UPDATE2: Each query provides 32 row, and here they are combined into 64 rows so I don't know which belongs to which query, you can see that USA is the last row of each query and then it starts with Algeria again for the second query with different values that do not represent the column description.
What I want is something like this:
+------+--------+ +------+--------+
| code | SUM(*) | | code | SUM(*) |
+------+--------+ +------+--------+
| AAA | 4 | | AAA | 4 |
| BBB | 3 | | CCC | 1 |
+------+--------+ +------+--------+
Then I did some searching in order to use JOIN as shown here Combine results of two unrelated queries into single view but, this scenario is much less complicated than mine and couldn't apply it on my scenario, Any Idea?
One view doesn't product two result sets. But you can identify where they come from:
CREATE VIEW TEAM_SUMMARY AS
SELECT 'Team1' as which,
c.country_name AS CountryName_T1, count(Team1) AS NoOfGames,
SUM(Team1_score) AS TotalGoalsFor,
SUM(Team2_score) AS TotalGoalsAgainst
FROM COUNTRY c JOIN
MATCH_RESULTS mr
ON c.country_name = mr.Team1
GROUP BY country_name
UNION ALL
SELECT 'Team2' as which,
c.country_name AS CountryNameT_2,
count(Team2) AS NoOfGames,
SUM(Team2_score) AS TotalGoalsFor,
SUM(Team1_score) AS TotalGoalsAgainst
FROM COUNTRY c JOIN
MATCH_RESULTS mr
ON c.country_name = mr.Team2
GROUP BY country_name;
Notes:
SELECT DISTINCT with GROUP BY is almost always unnecessary (as in this case.
Use UNION ALL by default. Only use UNION when you specifically want to incur the overhead for removing duplicates.
Table aliases make the query easier to write and to read.
The above adds a column which to specify where each row comes from.

self join with sum field

I have data like this
table t_prod
+---------+----------+-------------+-----------+----------+
|t_prod_id|t_prod_lot|t_prod_sublot|t_prod_card|t_prod_qty|
+---------+----------+-------------+-----------+----------+
| 4 | R001 | 1 | 1 | 6000 |
| 5 | R001 | 1 | 2 | 6000 |
| 6 | R001 | 1 | 3 | 6000 |
| 10 | R001 | 1 | 4 | 6000 |
| 11 | R001 | 1 | 5 | 6000 |
| 12 | R001 | 1 | 6 | 6000 |
| 13 | R001 | 2 | 1 | 6000 |
| 34 | R001 | 2 | 2 | 6000 |
| 36 | R001 | 2 | 3 | 2000 |
+---------+----------+-------------+-----------+----------+
and i want result like this when i select t_prod_lot = R001. count t_prod_sublot when same value and sum t_prod_qty when t_prod_sublot same value
+---------+----------+-------------+-----------+----------+--------------------+---------------+
|t_prod_id|t_prod_lot|t_prod_sublot|t_prod_card|t_prod_qty|count(t_prod_sublot)|sum(t_prod_qty)|
+---------+----------+-------------+-----------+----------+--------------------+---------------+
| 4 | R001 | 1 | 1 | 6000 | 6 | 36000 |
| 5 | R001 | 1 | 2 | 6000 | 6 | 36000 |
| 6 | R001 | 1 | 3 | 6000 | 6 | 36000 |
| 10 | R001 | 1 | 4 | 6000 | 6 | 36000 |
| 11 | R001 | 1 | 5 | 6000 | 6 | 36000 |
| 12 | R001 | 1 | 6 | 6000 | 6 | 36000 |
| 13 | R001 | 2 | 1 | 6000 | 3 | 14000 |
| 34 | R001 | 2 | 2 | 6000 | 3 | 14000 |
| 36 | R001 | 2 | 3 | 2000 | 3 | 14000 |
+---------+----------+-------------+-----------+----------+--------------------+---------------+
what is the query to produce this result ?
Sql Fiddle
Here's one option using a join with a subquery to count and sum the values:
select *
from t_prod t join (
select t_prod_lot, t_prod_sublot,
count(t_prod_card) t_prod_card_cnt,
sum(t_prod_qty) t_prod_qty_sum
from t_prod
group by t_prod_lot, t_prod_sublot
) t2 on t.t_prod_lot = t2.t_prod_lot and t.t_prod_sublot = t2.t_prod_sublot
Updated SQL Fiddle

How to merge tables in MySQL, although not just an outright merge?

I have three tables titled 'artist', 'album', and 'track' in MySQL, each containing information pertaining to my music library. 'artist' has the name of 4 artists, 'album' has the titles of 5 albums from those artists, and 'track' has the name of all tracks on said albums. I would like to combine the tables so the each track name has beside it the correct album title and artist name. Here are my tables:
artist
+-----------+-----------------+
| artist_id | artist_name |
+-----------+-----------------+
| 1 | Kacey Musgraves |
| 2 | The Lone Bellow |
| 3 | Ed Sheeran |
| 4 | The Civil Wars |
+-----------+-----------------+
album
+-----------+----------+-----------------------+
| artist_id | album_id | album_name |
+-----------+----------+-----------------------+
| 1 | 1 | Pageant Material |
| 2 | 1 | Then Came the Morning |
| 3 | 1 | + |
| 3 | 2 | x |
| 4 | 1 | The Civil Wars |
+-----------+----------+-----------------------+
track
+-----------+----------+----------+-------------------------------+------------+
| artist_id | album_id | track_id | track_name | track_time |
+-----------+----------+----------+-------------------------------+------------+
| 1 | 1 | 1 | Makin' Music for Money | 2.97 |
| 1 | 1 | 2 | Dime Store Cowgirl | 3.58 |
| 1 | 1 | 3 | Late to the Party | 3.63 |
| 1 | 1 | 4 | Pageant Material | 3.90 |
| 1 | 1 | 5 | This Town | 2.95 |
| 1 | 1 | 6 | Biscuits | 3.28 |
| 1 | 1 | 7 | Somebody to Love | 3.22 |
| 1 | 1 | 8 | Miserable | 3.00 |
| 1 | 1 | 9 | Die Fun | 3.47 |
| 1 | 1 | 10 | Family is Family | 2.53 |
| 1 | 1 | 11 | Good Ol' Boys Club | 3.27 |
| 1 | 1 | 12 | Cup of Tea | 2.67 |
| 1 | 1 | 13 | Fine | 7.88 |
| 2 | 1 | 1 | Then Came the Morning | 4.01 |
| 2 | 1 | 2 | Fake Roses | 3.57 |
| 2 | 1 | 3 | Marietta | 3.57 |
| 2 | 1 | 4 | Take My Love | 3.25 |
| 2 | 1 | 5 | Call to War | 3.47 |
| 2 | 1 | 6 | Watch Over Us | 3.57 |
| 2 | 1 | 7 | Diners | 4.40 |
| 2 | 1 | 8 | Heaven Don't Call Me Home | 2.60 |
| 2 | 1 | 9 | If You Don't Love Me | 3.23 |
| 2 | 1 | 10 | Telluride | 4.28 |
| 2 | 1 | 11 | To the Woods | 2.00 |
| 2 | 1 | 12 | Cold As It Is | 2.93 |
| 2 | 1 | 13 | I Let You Go | 3.42 |
| 3 | 1 | 1 | The A Team | 4.66 |
| 3 | 1 | 2 | Drunk | 4.32 |
| 3 | 1 | 3 | U.N.I. | 3.82 |
| 3 | 1 | 4 | Grade 8 | 3.00 |
| 3 | 1 | 5 | Wake Me Up | 3.83 |
| 3 | 1 | 6 | Small Bump | 4.32 |
| 3 | 1 | 7 | This | 3.27 |
| 3 | 1 | 8 | The City | 3.90 |
| 3 | 1 | 9 | Lego House | 3.08 |
| 3 | 1 | 10 | You Need Me, I Don't Need You | 3.67 |
| 3 | 1 | 11 | Kiss Me | 4.68 |
| 3 | 1 | 12 | Give Me Love | 8.77 |
| 3 | 2 | 1 | One | 4.22 |
| 3 | 2 | 2 | I'm A Mess | 4.08 |
| 3 | 2 | 3 | Sing | 3.92 |
| 3 | 2 | 4 | Don't | 3.67 |
| 3 | 2 | 5 | Nina | 3.77 |
| 3 | 2 | 6 | Photograph | 4.32 |
| 3 | 2 | 7 | Bloodstream | 5.00 |
| 3 | 2 | 8 | Tenerife Sea | 4.02 |
| 3 | 2 | 9 | Runaway | 3.42 |
| 3 | 2 | 10 | The Man | 4.17 |
| 3 | 2 | 11 | Thinking Out Loud | 4.70 |
| 3 | 2 | 12 | Afire Love | 5.23 |
| 3 | 2 | 13 | Take It BAck | 3.47 |
| 3 | 2 | 14 | Shirtsleeves | 3.17 |
| 3 | 2 | 15 | Even My Dad Does Sometimes | 3.82 |
| 3 | 2 | 16 | I See Fire | 4.98 |
| 4 | 1 | 1 | The One That Got Away | 3.55 |
| 4 | 1 | 2 | I Had Me a Girl | 3.75 |
| 4 | 1 | 3 | Same Old Same Old | 3.80 |
| 4 | 1 | 4 | Dust to Dust | 3.83 |
| 4 | 1 | 5 | Eavesdrop | 3.58 |
| 4 | 1 | 6 | Devil's Backbone | 2.48 |
| 4 | 1 | 7 | From This Valley | 3.55 |
| 4 | 1 | 8 | Tell Mama | 3.80 |
| 4 | 1 | 9 | Oh Henry | 3.55 |
| 4 | 1 | 10 | Disarm | 4.70 |
| 4 | 1 | 11 | Sacred Heart | 3.32 |
| 4 | 1 | 12 | D'Arline | 3.10 |
+-----------+----------+----------+-------------------------------+------------+
In 'track' each number under artist_id corresponds to the same numbered entry in 'artist', and the same goes for album_id and 'album'. Basically, I want to create a new table with every track in 'track', along with its respective album and artist.Any help would be appreciated!
Use JOIN in your SELECT query, like,
select * from track_name
join artist_name on track_name.artist_id = artist_name.artist_id
join album_name on track_name.album_id = album_name.album_id;
Now if you want this in new table you can insert this records in that table, or better if you want to run above query multiple time then you can create a VIEW.
MySQL Views
That sounds like a very basic join
select *
from track_name
join artist_name using (artist_id)
join album_name using (album_id)
I am writing this query by hoping there would be columns album_id and artist_id in the respective tables.
I also suggest you to change the column name track_name so to avoid conflict between table name and column name.
SELECT *
FROM `track_name` t1
LEFT JOIN `artist_name` a1 ON t1.`artist_id`=a1.`artist_id`
LEFT JOIN `album_name` a2 ON t1.`album_name`=a2.`album_name`
GROUP BY t1.`track_name`;