How to change the integer to string in a lookup table? - mysql

I cannot understand why joining tables are so difficult for me... But i would like to change a foreign key (platform_id) to the name from the lookup table. Dont know if this is the best approache, but should give me the best scaling options in the future.
Table companys
+----+--------+-------------+
| id | name | platform_id |
+----+--------+-------------+
| 1 | apple | 1 |
| 2 | google | 2 |
+----+--------+-------------+
Table platforms
+----+----------+-------------------+
| id | name | data |
+----+----------+-------------------+
| 1 | Software | {'market cap':''} |
| 2 | Retail | {'market cap':''} |
| 3 | Medicin | {'market cap':''} |
+----+----------+-------------------+
Expected result
+----+--------+-------------+
| id | name | platform_id |
+----+--------+-------------+
| 1 | apple | software |
| 2 | google | retail |
+----+--------+-------------+
What if i would like to also have the data column from platsforms table?

Use JOIN & create a view :
CREATE VIEW company_view
AS
SELECT c.id, c.name, p.name as platform_id, p.data
FROM company c INNER JOIN
platform p
ON p.id = c.platform_id;
If you want to change foreign key constraint to string value instead of platform_id. So, it is not advisable. Think if you need to change the name of platforms, you need to change ever where instead of one table.

Related

Is it possible to join multiple tables with one to many relations to the primary table without duplicated records?

Person Table
+----+------+
| id | name |
+----+------+
| 1 | John |
+----+------+
Car Table
+----+----------+
| id | car |
+----+----------+
| 1 | Audi |
| 1 | BMW |
| 1 | Mercedes |
+----+----------+
Credit_Card Table
+----+-----------------+
| id | bank |
+----+-----------------+
| 1 | Scotiabank |
| 1 | Bank of America |
+----+-----------------+
the id in Person table is the foreign key for the other two, I wonder if there is a way to join these tables together without showing duplicated records like this:
+----+------+----------+-----------------+
| id | name | car | bank |
+----+------+----------+-----------------+
| 1 | John | Audi | Scotiabank |
|NULL| NULL | BMW | Bank of America|
|NULL| NULL | Mercedes | NULL |
+----+------+----------+-----------------+
You can do this, but you shouldn't. Instead, I'l recommend putting all the values in one row:
select p.id, p.name,
(select group_concat(c.car)
from car c
where c.id = p.id
) as cars,
(select group_concat(b.bank)
from banks b
where b.id = p.id
) as banks
from person p ;
Why shouldn't you put the result in multiple rows as you specify? Well, it is definitely possible. However, the columns in a row should "related" to each other (this is much of what the "relational" in "relational algebra" is talking about). When you replace the first columns with NULL, then they no longer have any relationship to the rest of the columns. Similarly, the values in the last two columns have nothing to do with each other.
Plus, I'm guessing that the above pretty much does what you really want.

Replace foreign key IDs in table with values from those tables

I have a database I extracted from an application (I did not create it). I need to extract the data to use in a different application. The main table contains 3 foreign keys each linking to other tables. I want to replace the FK IDs with a specific column value from those other tables. The main table looks like this:
data:
+------------+------------+--------+----------+-----------+----------+------+
| startdate | enddate | status | employee | cause | duration | type |
+------------+------------+--------+----------+-----------+----------+------+
| 2018-01-05 | 2018-01-07 | 2 | 1 | some text | 3 | 1 |
+------------+------------+--------+----------+-----------+----------+------+
| 2018-02-03 | 2018-02-04 | 1 | 2 | some text | 2 | 2 |
+------------+------------+--------+----------+-----------+----------+------+
status, employee and type are all foreign keys. They link to tables with the following layouts:
users:
+----+-----------+----------+
| id | firstname | lastname |
+----+-----------+----------+
| 1 | John | Doe |
+----+-----------+----------+
| 2 | Dylan | Smith |
+----+-----------+----------+
types:
+----+-----------+
| id | name |
+----+-----------+
| 1 | Annual |
+----+-----------+
| 2 | Quarterly |
+----+-----------+
status:
+----+----------+
| id | name |
+----+----------+
| 1 | Accepted |
+----+----------+
| 2 | Rejected |
+----+----------+
I want the main table to then look like this is
+------------+------------+-----------+--------------+-----------+----------+-----------+
| startdate | enddate | status | employee | cause | duration | type |
+------------+------------+-----------+--------------+-----------+----------+-----------+
| 2018-01-05 | 2018-01-07 | Rejected | John Doe | some text | 3 | Annual |
+------------+------------+-----------+--------------+-----------+----------+-----------+
| 2018-02-03 | 2018-02-04 | Accepted | Dylan Smith | some text | 2 | Quarterly |
+------------+------------+-----------+--------------+-----------+----------+-----------+
I've tried various things I've found on Google as well as SO but I can't get it to come out the way I want it to. I come from a PHP background but I have some experience in SQL, but I am not an expert. I've tried getting the values corresponding to the IDs and saving them in a different table, but I don't know how I can update the table with those returned values:
INSERT into stat
SELECT b.name stat
FROM data a JOIN status b
ON a.status = b.id
This creates a column with the right values in the right order in a table called "stat", like so:
+----------+
| status |
+----------+
| Rejected |
+----------+
| Accepted |
+----------+
This works similarly for the "users" (I also concatenate the first name and last names into a column called "name" in a "user" table. I want to use the concatenated value) and "types" tables. How can I update each respective FK value in the "data" table with their respective values as in the tables above?
Joining from the data table to the three other tables should work here:
SELECT
d.startdate,
d.enddate,
s.name AS status,
CONCAT(u.firstname, ' ', u.lastname) AS employee,
d.cause,
d.duration,
t.name AS type
FROM data d
LEFT JOIN status s
ON d.status = s.id
LEFT JOIN users u
ON d.employee = u.id
LEFT JOIN types t
ON d.type = t.id;
If a given value coming in from one of the joins be missing, and you want to display a placeholder value (instead of the default NULL), then you may use the COALESCE function.

mysql explode a field loop through the values and create a new column with the title associated with those values

I have two tables users and services and i am try to write a single query that will create a new column skills. The values in the column skills will be the service_title which maps to the service_id stored in user_skills.
Below are the examples of a the tables used:
Table users:
+---------+---------------+----------------+----------------+
| id | user_fname | user_lname | user_skills |
+---------+---------------+----------------+----------------+
| 1 | kiran | bhattarai | 1,2 |
| 2 | sujan | bhattarai | 2,4 |
| 3 | manorath | dad | 1,2,3,4 |
| 4 | bhagawoti | mom | 2,3 |
+---------+---------------+----------------+----------------+
Table services:
+-----------------+------------------+
| service_id | service_title |
+-----------------+------------------+
| 1 | cook |
| 2 | clean |
| 3 | grocessery |
| 4 | teach |
+-----------------+------------------+
Currently i am using this query:
SELECT users.user_fname,
users.user_lname,
(SELECT GROUP_CONCAT(service_title)
FROM `services`
WHERE `service_id` IN (1,2,3,4)) as skills
FROM users
WHERE
id =3;
Result of the above query:
+---------------+----------------+----------------------------------------+
| user_fname | user_lname | skills |
+---------------+----------------+----------------------------------------+
| manorath | dad | cook,clean,grocessery,teach |
+---------------+----------------+----------------------------------------+
Instead of using the IN (1,2,3,4) I tried IN (users.user_skills) because the values in user_skills changes all the time and the result was:
+---------------+----------------+----------------------------------------+
| user_fname | user_lname | skills |
+---------------+----------------+----------------------------------------+
| manorath | dad | cook |
+---------------+----------------+----------------------------------------+
Every time a new service is added i have to add that service_id in the IN (1,2,3,4,new service id) of my query which is not a proper solution. I have already tried using php and another query to do solve this, the disadvantage of doing that is it is slowing down the process. How should i solve this problem in a single query.
You can use FIND_IN_SET() to JOIN the two tables.
SELECT
u.user_fname,
u.user_lname,
GROUP_CONCAT(s.service_title) as skills
FROM users u
LEFT JOIN services s
ON FIND_IN_SET(s.service_id, u.user_skills)
WHERE u.id = 3
Note that a JOIN with a FIND_IN_SET() condition cannot utilize any index. And that can lead to poor performance.
In general it's a bad idea to store relations in a separated string column. See Is storing a delimited list in a database column really that bad?.
You should normalize your design and create a separate table for your many-to-many relation. The table would look like
Table users_services:
+---------+------------+
| user_id | service_id |
+---------+------------+
| 1 | 1 |
| 1 | 2 |
| 2 | 2 |
| 2 | 4 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
| 3 | 4 |
| 4 | 2 |
| 4 | 3 |
+---------+------------+
And the query would be
SELECT
u.user_fname,
u.user_lname,
GROUP_CONCAT(s.service_title) as skills
FROM users u
LEFT JOIN users_services us ON us.user_id = u.id
LEFT JOIN services s ON s.service_id = us.service_id
WHERE u.id = 3

Mysql use another table to translate value

table user
+-----+-------+----------+------------+
| id | name | gender | computer |
+-----+-------+----------+------------+
| 1 | ben | 1 | 3 |
+-----+-------+----------+------------+
table translate
+-----+--------+
| id | trans |
+-----+--------+
| 1 | boy |
| 2 | girl |
| 3 | pc |
| 4 | mac |
+-----+--------+
final result
+-----+-------+----------+----------+
| id | name | gender | computer |
+-----+-------+----------+----------+
| 1 | ben | boy | pc |
+-----+-------+----------+----------+
I have 2 tables, one is user's data, the other is translate.
I want to use translate table to translate user's gender & computer's values
anyone know how to achieve this?
Use JOIN.
Query
select
t1.`id`,
t1.`name`,
t2.`trans` as `gender`,
t3.`trans` as `computer`
from `user` t1
join `translate` t2
on t1.`gender` = t2.`id`
join `translate` t3
on t1.`computer` = t3.`id`;
The way you've presented this, it doesn't appear to be a translation problem (in the sense of language), but rather a standard database partition problem. A more standard solution would be to define your entities each in their own tables:
user
+-----+--------+------------+--------------+
| id | name | genderId | computerId |
+-----+--------+------------+--------------+
| 1 | ben | 1 | 1 |
+-----+--------+------------+--------------+
gender
+-----+--------+
| id | name |
+-----+--------+
| 1 | boy |
+-----+--------+
| 2 | girl |
+-----+--------+
computer
+-----+--------+
| id | name |
+-----+--------+
| 1 | pc |
+-----+--------+
| 2 | mac |
+-----+--------+
...and then your query reads:
SELECT *
FROM user
INNER JOIN gender ON user.genderId = gender.id
INNER JOIN computer ON user.computerId = computer.id

build dynamic sql insert query to merge multiple tables into single table using mapping table

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 &nbsp... | 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