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

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

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;

how to store multiple id in multiple column

first table :
table Name : ssc_course;
+---------------+-----------------+------------+
| ssc_course_id | ssc_course_name | qualify_id |
+---------------+-----------------+------------+
| 1 | HSC | 1 |
| 2 | Diploma | 1 |
| 3 | ITI | 1 |
+---------------+-----------------+------------+
second table :
table name :ssc_stream
+---------------+--------------------+-----------------+
| ssc_stream_id | ssc_stream_name | ssc_course_id(fk) |
+---------------+--------------------+-----------------+
| 1 | Science | 1 |
| 2 | Commers | 1 |
| 3 | Arts | 1 |
| 17 | Computer Engg | 2 |
| 18 | Mechanical Engg | 2 |
| 19 | Civil Engg | 2 |
| 20 | Electronics & TELE | 2 |
| 21 | E&TC | 2 |
+---------------+--------------------+-----------------+
third table :(master table)
table name : ssc_college
+------------+--------------+---------------+---------------+-
| ssc_clg_id | clg_login_id | ssc_stream_id | ssc_course_id |
+------------+--------------+---------------+---------------+-
| 9 | 2 | 1 | 1 |
| 10 | 2 | 1 | 1 |
| 11 | 2 | 2 | 1 |
| 12 | 2 | 2 | 1 |
| 13 | 2 | 3 | 1 |
| 14 | 2 | 3 | 1 |
| 15 | 3 | 1 | 1 |
| 16 | 3 | 1 | 1 |
| 17 | 3 | 2 | 1 |
| 18 | 3 | 2 | 1 |
| 19 | 3 | 3 | 1 |
| 20 | 3 | 3 | 1 |
| 21 | 4 | 1 | 1 |
| 22 | 4 | 1 | 1 |
| 23 | 4 | 2 | 1 |
| 24 | 4 | 2 | 1 |
| 25 | 4 | 3 | 1 |
| 26 | 4 | 3 | 1 |
| 27 | 5 | 17 | 2 |
| 28 | 5 | 17 | 2 |
| 29 | 5 | 18 | 2 |
| 30 | 5 | 18 | 2 |
| 31 | 5 | 19 | 2 |
| 32 | 5 | 19 | 2 |
| 33 | 5 | 20 | 2 |
| 34 | 5 | 20 | 2 |
| 35 | 5 | 21 | 2 |
| 36 | 5 | 21 | 2 |
| 38 | 6 | 17 | 2 |
| 39 | 6 | 17 | 2 |
| 40 | 6 | 18 | 2 |
*************************************************************
I am trying to save course,stream id's into ssc_college table.
1 course have multiple stream how can i insert this type of values
ex. course 1-diploma and this course have multiple stream like-
1.comp. engg , 2.mechanical ,3.e&tc i want to add course id and
stream id at a time of third (ssc_college) table.
and multiple stream for multiple course how i can add at a time
multiple parent and multiple child id in mysql table.
Note: stream are dependant of course table.

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.

Creating a log having the date of purchase

I need to create a log having the purchase date of an item.
Items can be owned by only one buyer at time. So, for example, if item1 was purchased by buyer2 in 2009 and after by buyer1 in 2015, then between 2009 and 2015 was owned by buyer2.
Here is my table:
+--------+------------+-----------+----------+
| id_doc | date | id_item | id_buyer |
+--------+------------+-----------+----------+
| 11 | 2016-06-07 | 1 | 4 |
| 10 | 2016-06-06 | 1 | 4 |
| 1 | 2015-11-30 | 1 | 1 |
| 9 | 2009-01-01 | 1 | 2 |
| 4 | 2001-01-12 | 1 | 2 |
| 8 | 1996-06-06 | 1 | 2 |
| 3 | 1995-05-29 | 1 | 1 |
| 2 | 1998-05-23 | 2 | 2 |
| 7 | 2014-10-10 | 3 | 2 |
| 6 | 2003-12-12 | 3 | 3 |
| 5 | 1991-01-12 | 3 | 2 |
+--------+------------+-----------+----------+
Here is a kind of table/view I need:
+------------+------------+-----------+----------+--------+
| date_from | date_to | id_item | id_buyer | id_doc |
+------------+------------+-----------+----------+--------+
| 2016-06-07 | - | 1 | 4 | 11 |
| 2016-06-06 | 2016-06-07 | 1 | 4 | 10 |
| 2015-11-30 | 2016-06-06 | 1 | 1 | 1 |
| 2009-01-01 | 2015-11-30 | 1 | 2 | 9 |
| 2001-01-12 | 2009-01-01 | 1 | 2 | 4 |
| 1996-06-06 | 2001-01-12 | 1 | 2 | 8 |
| 1995-05-29 | 1996-06-06 | 1 | 1 | 3 |
| 1998-05-23 | - | 2 | 2 | 2 |
| 2014-10-10 | - | 3 | 2 | 7 |
| 2003-12-12 | 2014-10-10 | 3 | 3 | 6 |
| 1991-01-12 | 2003-12-12 | 3 | 2 | 5 |
+------------+------------+-----------+----------+--------+
I've tried a lot with GROUP BY, GROUP_CONCAT, trying to access next record date, etc ... but I can't found out how to solve the problem.
Thanks in advance.
I finally found out the solution only for past purchases.
SELECT
main.id_doc, main.id_item, main.date AS "date_from", bi.date AS "date_to", main.id_buyer
FROM
MyTable main, MyTable bi
WHERE
bi.id_doc =
(
SELECT sub.id_doc
FROM MyTable sub
WHERE sub.id_item = main.id_item AND sub.date > main.date ORDER BY sub.date ASC LIMIT 1
);