I have been trying to find duplicate and got confused by 3 lines of codes. Can you please help me understand it?
Please find DESC of my table :
| Field | Type | NULL | Key | Extra |
| id | int(11) | NO | PRI | auto_increment |
| name | varchar(50) | YES | | |
| membership | enum('Silver','Gold','Diamond') | YES | | |
| interest |set('Movie','Music','Concert') | YES | | |
This is the output for all the present data in table.
Select * from clients
+----+--------+------------+---------------+
| id | name | membership | interest |
+----+--------+------------+---------------+
| 1 | Sourav | Silver | Movie,Concert |
| 2 | Yash | Diamond | Music |
| 3 | Yash | Diamond | Music |
| 4 | Yash | Diamond | Music |
| 5 | Yash | Diamond | Music |
| 6 | Yash | Diamond | Music |
| 7 | Yash | Diamond | Music |
| 8 | Yash | Diamond | Music |
| 9 | Yash | Diamond | Music |
| 10 | Yash | Diamond | Music |
| 11 | Yash | Diamond | Music |
| 12 | Yash | Diamond | Music |
| 13 | Yash | Diamond | Music |
| 14 | Yash | Diamond | Music |
| 15 | Sneha | Silver | Concert |
+----+--------+------------+---------------+
15 rows in set (0.001 sec)
Confusion arose in below three queries for why do they return 3 completely different outputs?
1.
SELECT id, name, membership, interest, count(*)
FROM clients
GROUP BY name, membership, interest
HAVING count(*) > 1;
//This one being the correct line of code
+----+------+------------+----------+----------+
| id | name | membership | interest | count(*) |
+----+------+------------+----------+----------+
| 2 | Yash | Diamond | Music | 13 |
+----+------+------------+----------+----------+
SELECT name, membership, interest, count()
FROM clients
HAVING count() > 1;
//Returns first entry with count as 15
+--------+------------+---------------+----------+
| name | membership | interest | count(*) |
+--------+------------+---------------+----------+
| Sourav | Silver | Movie,Concert | 15 |
+--------+------------+---------------+----------+
SELECT id, name, membership, interest, count(*)
FROM clients
GROUP BY name, membership, interest;
//Returns Unique Entries only while eliminating the duplicate entries. However, Sneha (last entry) was on top for some reason
+----+--------+------------+---------------+----------+
| id | name | membership | interest | count(*) |
+----+--------+------------+---------------+----------+
| 15 | Sneha | Silver | Concert | 1 |
| 1 | Sourav | Silver | Movie,Concert | 1 |
| 2 | Yash | Diamond | Music | 13 |
+----+--------+------------+---------------+----------+
What necessary changes happened when I used statement because given same syntax without statement clause, it was working completely different.
In addition to that the last statement query was intriguing me as even though I did not apply any clause for Unique data, it delivered the same to me.
Finally, why did the last entry appeared at top in the 3. query?
Correct query finding dupes in your case should be (one possibility as might be other solution as well),
select min(id) as id,
name,
membership,
interest,
count(*) as cnt
from clients
group by name,membership,interest
having count(*) > 1;
https://dbfiddle.uk/HsV-S897
As per your tries.
The first query should fail with sql_mode only_full_group_by enabled which should be enabled .
In the second query HAVING without GROUP BY acts like WHERE, still the second query will fail with sql_mode only_full_group_by enabled.
The third query is correctly formed but you need order by count(*) desc to get Yash first.
Related
i have 2 tables.
1.home_shop
+---+------------+
|id | product |
+---+------------+
| 1 | soap |
| 2 | cake |
| 3 | biscuit |
+---+------------+
2.office_shop
+---+------------+
|id | product |
+---+------------+
| 1 | key |
| 2 | lock |
| 3 | pen |
+---+------------+
what i want is union this two tables into a new table "complete_shop" with a flag indicating "home" and "office"
for example:
+---+------------+-------------+
|id | product | flag |
+---+------------+-------------+
| 1 | soap | home |
| 1 | key | office |
| 2 | cake | home |
| 2 | lock | office |
| 3 | biscuit | home |
| 3 | pen | office |
+---+------------+-------------+
how do i do this union in mysql please help me. i am a beginner
Do a UNION query and introduce the flag column using the appropriate values.
SELECT id, product, 'home' AS flag
FROM home_shop
UNION ALL
SELECT id, product, 'office' AS flag
FROM office_shop
ORDER BY id, flag
Note that you don't need to use a subquery to order here, you can just specify the columns you want to use.
I am developing a movie database with both credits for actors and filmmakers. Currently, the actor and filmmaker credits are all together in one table because they only differ by one column (the "role_id" foreign key never gets used for filmmaker credits -- it is always NULL). Is it still best practice to keep them in one table ("movie_credits") or would it be better to separate ("actor_credits" and "filmmaker_credits")?
Here is a very basic schema I've defined, and I'm including some example data in it:
movie_credits
+----+-----------+---------+---------------+----------+
| id | talent_id | role_id | identity_id | title_id |
+----+-----------+---------+---------------+----------+
| 1 | 1 | 1 | 1 | 1 |
| 2 | 2 | 2 | 1 | 2 |
| 3 | 1 | NULL | 2 | 4 |
+----+-----------+---------+---------------+----------+
talent
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 1 | Clint | Eastwood |
| 2 | Sylvester | Stallone |
+----+------------+-----------+
roles
+----+---------------+
| id | name |
+----+---------------+
| 1 | Walt Kowalski |
| 2 | Rocky Balboa |
+----+---------------+
identities
+----+--------------+
| id | identity |
+----+--------------+
| 1 | actor |
| 2 | director |
| 3 | producer |
| 4 | screenwriter |
+----+--------------+
titles
+----+-----------------+
| id | name |
+----+-----------------+
| 1 | Gran Torino |
| 2 | Rocky |
| 3 | Creed |
| 4 | American Sniper |
+----+-----------------+
If you see a completely different way of structuring the data for this scenario, please let me know. I am open to any solution that may be even more efficient and scalable.
Basically, both actor and filmmaker are persons.
So they must be stored in a single table.
We must avoid null and redundancy from the database. But in this case, making the extra table will increase much overhead than the profit of removing null.
Also, you can simply assign filmmaker as the role to avoid nulls.
That is, there will be a role named filmmaker.
I'm new to mySQL, and I'm trying to be able to either SELECT or CREATE A VIEW with the information I want to retrieve from two tables. SQLfiddle
People Table
| people_id | username | Password | Email |
----------------------------------------------
| 1 | username1 | Password1 | Email1 |
| 2 | username2 | Password2 | Email2 |
| 3 | username3 | Password3 | Email3 |
Profile Table
| people_id | id | age | location | hobbies | about |
----------------------------------------------------------------------------------------------------------------------------
| 1 | 1 | 22 | USA | skiing, snowboarding | I am from the US and I like to snowboard |
| 2 | 2 | 45 | Canada | curling, ice skating, reading | I like to ice skate! |
| 3 | 3 | 38 | USA | tv, movies, you name it | I am from the US and I like to watch the tube |
| 2 | 4 | 45 | Canada | curling, reading | I do not like to ice skate anymore |
| 2 | 5 | 46 | Canada | bowling | Bowling is my new favorite hobby! I just turned 46! |
| 1 | 6 | 22 | Brazil | skiing, snowboarding | I am orginally from the US but I just moved to brazil|
I would like to see/retrieve the data like this :
| people_id | username | age | location | hobbies | about |
------------------------------------------------------------------------------------------------------------------------------------
| 3 | username3 | 38 | USA | tv, movies, you name it | I am from the US and I like to watch the tube |
| 2 | username2 | 46 | Canada | bowling | Bowling is my new favorite hobby! I just turned 46! |
| 1 | username1 | 22 | Brazil | skiing, snowboarding | I am orginally from the US but I just moved to brazil|
So I need to select all the people_id and username from table People and then select the people_id row from Profile where the id is the largest number for each people_id
I've tried
SELECT People.people_id, People.username, Profile.age, Profile.location, Profile.hobbies, Profile.about
FROM People
INNER JOIN Profile
ON People.people_id=Profile.people_id
Which gets me closer to what I want, but I don't want to show duplicate rows, I only want to show the last row inserted into the Profile table for each people_id.
SQLfiddle
The most efficient way to get what you want is to use a not exists condition in the where clause. This definitely takes some getting used to. What the query is going to do is to get the matching row from Profile subject to the condition that no other row has a larger id. This is a round-about way of saying "get the biggest id for each person". But, it happens to produce an efficient query plan (and this is true in most databases, not just MySQL).
SELECT p.people_id, p.username, pr.age, pr.location, pr.hobbies, pr.about
FROM People p INNER JOIN
Profile pr
ON p.people_id = pr.people_id
WHERE NOT EXISTS (SELECT 1
FROM Profile pr2
WHERE pr2.people_id = pr.people_id AND
pr2.id > pr.id
);
SELECT People.people_id, People.username, Profile.age, Profile.location, Profile.hobbies, Profile.about
FROM People
INNER JOIN (SELECT * FROM Profile ORDER BY id DESC) AS Profile
ON People.people_id=Profile.people_id
GROUP BY People.people_id
ORDER BY people_id DESC
I am looking to take data from multiple mysql tables with different structures and insert it into one master table containing different columns/data from the sources,
I have created a column name mapping table with the following data:
TABLE: affiliate_datafeed_magento_mapping
+----+-------------+----------------+
| id | datafeed | magento |
+----+-------------+----------------+
| 1 | SKU | sku |
| 1 | Name | name |
| 1 | Description | description |
| 1 | Url | affiliate_link |
| 1 | Price | price |
| 1 | Brand | brand |
| 1 | ModelNumber | model |
| 2 | SKU | sku |
| 2 | Name | name |
| 2 | Description | description |
| 2 | Url | affiliate_link |
| 2 | Price | price |
| 2 | Currency | affiliate_cur |
+----+-------------+----------------+
id corresponds to the id of the affiliate program in the table affiliate_programs
datafeed corresponds the column name in the source table with the corresponding id in the table affiliate_program
magento corresponds the the column name in the table to receive the data
TABLE: affiliate_programs
+----+-------------------+-------------------------+------------------------+------------------------+-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+-------------------------------------+-------+
| id | affiliate_network | affiliate_network_short | affiliate_program | affiliate_program_long | affiliate_program_short | affiliate_program_datafeed_url | affiliate_program_datafeed_csv_filename | affiliate_program_datafeed_last_get | order |
+----+-------------------+-------------------------+------------------------+------------------------+-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+-------------------------------------+-------+
| 1 | CommissionFactory | cf_ | Lifestyle Clotheslines | lifestyleclotheslines | lcl | http://dashboard.commissionfactory.com.au/Affiliate/Creatives/DataFeeds/jPuB5NPigbec7prnjLCX5MHygO$A5sTlhOHb8t3nmufzta#k5uyn5qqxrLDr86GysLTj$bTyoKaj77Pltfmh9dvnkOCS4MHzjvTSlK6Dfg==/ | cf_lifestyleclotheslines.csv | 2013-08-12 22:23:02 | NULL |
| 2 | CommissionFactory | cf_ | MacArthur Baskets | macarthurbaskets | mcb | http://dashboard.commissionfactory.com.au/Affiliate/Creatives/DataFeeds/jPSA4dbg17SY7svvjeSX5Jf1iO#b5JXshOfY#ovjzeKj4PGivuyn5qqxrLDr86GysLTj$bTyoKaj77Pltfmh9dvnkOCS4MHzjvTSlK6Dfg==/ | cf_macarthurbaskets.csv | 2013-08-12 22:23:02 | NULL |
| 3 | ClixGalore | cg_ | Boutique Wineries | boutiquewineries | btw | http://www.is1.clixGalore.com/DataFileRequest.aspx?AdID=9522&AfID=264058 | cg_boutiquewineries.csv | 2013-08-12 22:23:02 | NULL |
| 4 | ClixGalore | cg_ | Brewtopia | brewtopia | bwt | http://www.is1.clixGalore.com/DataFileRequest.aspx?AdID=4817&AfID=264058 | cg_brewtopia.csv | 2013-08-12 22:23:02 | NULL |
| 5 | ClixGalore | cg_ | MacArthur Baskets | macarthurbaskets | mcb2 | http://www.is1.clixGalore.com/DataFileRequest.aspx?AdID=4909&AfID=264058 | cg_macarthurbaskets.csv | 2013-08-12 22:23:02 | NULL |
| 6 | ClixGalore | cg_ | Winemakers Choice | winemakerschoice | wmc | http://www.is1.clixGalore.com/DataFileRequest.aspx?AdID=4282&AfID=264058 | cg_winemakerschoice.csv | 2013-08-12 22:23:02 | NULL |
+----+-------------------+-------------------------+------------------------+------------------------+-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+-------------------------------------+-------+
id corresponds to the same id in the table affiliate_datafeed_magento_mapping
the concatenation of affiliate_programs.affiliate_network_short and affiliate_programs.affiliate_program_long make up the name of the source table
TABLE: cf_lifestyleclotheslines
+----+---------------------+---------------------+------+------------------------------------------------------+--------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------+---------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+-------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+--------------+-------------+-------------+
| id | DateCreated | DateModified | SKU | Name | Category | Description | Url | OriginalUrl | Image | Image50 | Image100 | Image120 | Image200 | Image300 | Image400 | Price | Brand | ModelNumber |
+----+---------------------+---------------------+------+------------------------------------------------------+--------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------+---------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+-------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+--------------+-------------+-------------+
| 1 | 2012-10-19 10:52:50 | 2013-06-20 02:07:37 | 30 | Airaus Ceiling Mounted Clothes Airer | Home & Garden > Household Supplies > Laundry Supplies > Drying Racks & Hangers | Watch the Product Video Just arrived from Europe! Lifestyle Clotheslines is now stocking the latest ceiling mounted clothes airers from Europe. Ceiling mounted clothes airers are the perfect alternative for those with limited space situations or who need the benefit of an indoor clothesline or airer. Boasting 6 individually adjustable drying rods which are made of steel not plastic, these indoor airers can be used in a range of situations and are ideally suited for the disabled as they can easily be lowered or raised to the height of the user. An indoor airer can also help extend the life of your clothes by reducing the exposure to harsh UV rays and they can also be a big power saver for your home when used to dry your washing instead of a tumble dryer. Discover today how one of our new ceiling mounted clothes airers can save you money and take all the hard work and fuss out of drying your washing. // Customer Video Reviews  ... | https://track.commissionfactory.com.au/p/10604/1718691 | http://www.lifestyleclotheslines.com.au/airaus-ceiling-mounted-clothes-airer/ | http://content.commissionfactory.com.au/Products/7228/1718691.jpg | http://content.commissionfactory.com.au/Products/7228/1718691#50x50.jpg | http://content.commissionfactory.com.au/Products/7228/1718691#100x100.jpg | http://content.commissionfactory.com.au/Products/7228/1718691#120x120.jpg | http://content.commissionfactory.com.au/Products/7228/1718691#200x200.jpg | http://content.commissionfactory.com.au/Products/7228/1718691#300x300.jpg | http://content.commissionfactory.com.au/Products/7228/1718691#400x400.jpg | 99.0000 AUD | | AIRAUS |
+----+---------------------+---------------------+------+------------------------------------------------------+--------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------+---------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+-------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+--------------+-------------+-------------+
So with the data provided I'm looking to go through all the tables (concatenation of affiliate_programs.affiliate_network_short and affiliate_programs.affiliate_program_long columns in affiliate_programs and build a insert query for them all based on the mapped columns in the table affiliate_datafeed_magento_mapping to insert the data into the table magento_export
Any assistance would be awesome!
Tim
I dont think you need the mapping table at all
I would use a series of UNION queries. One query per source. In the SELECT clause for each of those queries you should have a 1:1 column for each coulmn in your destination table, including mapping the value NULL where that destination column is not applicable to that source. (This is where the mapping really happens)
Then your INSERT statement is a straight-forward mapping of all the UNIONED queries into the destination table.
Pseudo code
INSERT INTO Destination(Col1, Col2, Col3, Col4)
SELECT T1.Field1 as Col1
,NULL as Col2
,T1.Field7 as Col3
,T1.Field2 as Col4
FROM FirstSource T1
UNION
SELECT T2.Field1 as Col1
,Some Calulation as Col2
,T2.Field3 as Col3
,NULL as Col4
FROM FirstSource T1
I have been given some legacy mysql database (about 50k rows) to work with. Basically, the database table has the following structure (flat structure, no foreign keys)
-----------------------------------------------------------------
| IDENTIFIER | Release Year | Category | Album Reviews | Artist |
-----------------------------------------------------------------
| 1 | 1981 | Rock | abc.com | GNR |
-----------------------------------------------------------------
| 2 | 2000 | Pop | xyz.com | JayZ |
-----------------------------------------------------------------
| 3 | 2001 | Pop | jkl.com | Spears |
-----------------------------------------------------------------
| 4 | 1981 | Rock | onm.com | GNR |
-----------------------------------------------------------------
| 5 | 2000 | Pop | qwe.com | JayZ |
-----------------------------------------------------------------
| 6 | 2001 | Pop | vgh.com | Spears |
-----------------------------------------------------------------
As you can see, each artist, has an album released in a particular year, and the album reviews are available on pages specified in column "Album Reviews". However, the table has some "duplicates", which is that there are multiple "Album Review's" for each artist.
What I would like is to arrange the above mysql table as follows (assuming I want to restrict myself to only 2 album reviews):
---------------------------------------------------------------------------------
| IDENTIFIER | Release Year | Category | Album Reviews1 | Album Reviews2 | Artist |
---------------------------------------------------------------------------------
| 1 | 1981 | Rock | abc.com | onm.com | GNR |
---------------------------------------------------------------------------------
| 2 | 2000 | Pop | xyz.com | qwe.com | JayZ |
---------------------------------------------------------------------------------
| 3 | 2001 | Pop | jkl.com | vgh.com | Spears |
---------------------------------------------------------------------------------
Also, there could be multiple (more than 2 and upto 5) album reviews. The problem is I cant use any foreign keys (for the curious, it is because the db table will be later used with django and haystack later).
Is it possible to achieve the above structure? I did try googling around, but couldn't find any sufficient material on how to achieve this.
I would appreciate any guidance on this.
To make this simplier, imagine we remove the album reviews columns from both tables. In your example, you ASSUMED that every equal tuple (release year, category and artist) referred to the same album, hence, you created the result with 3 final records. However, as you know, nobody can assume that, so your own example is wrong, because Spears could have had 2 different albums in 2001 for the same category :)
Additionally you might have the same album twice for the same year, but with different categories (this would actually depend on how data was inserted there).
So, what's the problem? As long as you can't find a way to identify each album in your current data there is no way to derive a new table grouped by albums.
Edit:
"you ASSUMED that every equal tuple (release year, category and artist) referred to the same album" ofcourse, there are assumptions and these assumptions are valid for my test case (if that makes sense).
Ok, if those assumptions are valid, then, given the following set of data (I've added some records):
+----+-------------+----------+--------------+--------+
| ID | RELEASEYEAR | CATEGORY | ALBUMREVIEWS | ARTIST |
+----+-------------+----------+--------------+--------+
| 1 | 1981 | Rock | abc.com | GNR |
| 2 | 2000 | Pop | xyz.com | JayZ |
| 3 | 2001 | Pop | jkl.com | Spears |
| 4 | 1981 | Rock | onm.com | GNR |
| 5 | 2000 | Pop | qwe.com | JayZ |
| 6 | 2001 | Pop | vgh.com | Spears |
| 7 | 2001 | Pop | ppp.com | Spears |
| 8 | 2003 | Rock | zzz.com | Stones |
| 9 | 2007 | Pop | ppp.com | Spears |
+----+-------------+----------+--------------+--------+
You can get to this result:
+-------------+----------+---------------+---------------+--------+
| RELEASEYEAR | CATEGORY | ALBUMREVIEWS1 | ALBUMREVIEWS2 | ARTIST |
+-------------+----------+---------------+---------------+--------+
| 1981 | Rock | abc.com | onm.com | GNR |
| 2000 | Pop | xyz.com | qwe.com | JayZ |
| 2001 | Pop | jkl.com | vgh.com | Spears |
| 2003 | Rock | zzz.com | | Stones |
| 2007 | Pop | ppp.com | | Spears |
+-------------+----------+---------------+---------------+--------+
By running the following query:
select s1.releaseYear, s1.category, max(s1.albumReviews1) as AlbumReviews1,
max(s1.albumReviews2) as AlbumReviews2, s1.artist
from (
select t1.releaseYear, t1.category, t1.artist,
if ((
select count(*) from t t2
where t2.releaseYear = t1.releaseYear and t2.category = t1.category and
t2.artist = t2.artist and t2.id < t1.id
) = 0, t1.albumReviews, null) as AlbumReviews1,
if ((
select count(*) from t t2
where t2.releaseYear = t1.releaseYear and t2.category = t1.category and
t2.artist = t2.artist and t2.id < t1.id
) = 1, t1.albumReviews, null) as AlbumReviews2
from t t1
) as s1
group by s1.releaseYear, s1.category, s1.artist
Give it a try and let me know how it works.
What you can do is Create two tables.
First one is as below.
-------------------------------------------------
| IDENTIFIER | Release Year | Category | Artist |
-------------------------------------------------
| 1 | 1981 | Rock | GNR |
-------------------------------------------------
| 2 | 2000 | Pop | JayZ |
-------------------------------------------------
| 3 | 2001 | Pop | Spears |
-------------------------------------------------
NOTE : In table 1, IDENTIFIER is PRIMARY KEY.
Second table will be as below.
------------------------------
| IDENTIFIER | Album Reviews |
------------------------------
| 1 | abc.com |
------------------------------
| 1 | onm.com |
------------------------------
| 2 | xyz.com |
------------------------------
| 2 | qwe.com |
------------------------------
| 3 | jkl.com |
------------------------------
| 3 | vgh.com |
------------------------------
NOTE : In table 2, IDENTIFIER is FOREIGN KEY.
This way you can have many records of Album Reviews for IDENTIFIER (artist for that year).
Let me know if you still have any questions.