MySQL combining outer joins - mysql

For this question I have created a simple example that illustrates what I am asking.
Say I had a table called 'books'
+----+----------------------------+-----------+
| pk | title | author_id |
+----+----------------------------+-----------+
| 1 | The Lost Symbol | 1 |
| 2 | Follow Us Home | 2 |
| 3 | The Man in the High Castle | 3 |
+----+----------------------------+-----------+
(table a)
And another table called 'shops', that had a list of shops that sold each book:
+----+---------+-------------+-------+
| pk | book_id | shop_name | price |
+----+---------+-------------+-------+
| 1 | 1 | WHSmith | 5.00 |
| 2 | 1 | Waterstones | 7.00 |
| 3 | 1 | Amazon | 2.50 |
| 4 | 2 | WHSmith | 4.00 |
| 5 | 2 | Borders | 4.50 |
+----+---------+-------------+-------+
(table b)
If I do a simple select that grabs a book and all of the places it is sold using a join such as:
SELECT
books.*,
shops.shop_name,
shops.price
FROM
books
JOIN shops ON books.pk = shops.book_id
WHERE
book.book_name = "The Lost Symbol"
I would get results such as below:
+----+-----------------+-----------+-------------+-------+
| pk | title | author_id | shop_name | price |
+----+-----------------+-----------+-------------+-------+
| 1 | The Lost Symbol | 1 | WHSmith | 5.00 |
| 1 | The Lost Symbol | 1 | Waterstones | 7.00 |
| 1 | The Lost Symbol | 1 | Amazon | 2.50 |
+----+-----------------+-----------+-------------+-------+
(table c)
However, I would LIKE to receive results like this:
+----+-----------------+-----------+-------------+-------+
| pk | title | author_id | shop_name | price |
+----+-----------------+-----------+-------------+-------+
| 1 | The Lost Symbol | 1 | NULL | NULL |
| 1 | The Lost Symbol | 1 | WHSmith | 5.00 |
| 1 | The Lost Symbol | 1 | Waterstones | 7.00 |
| 1 | The Lost Symbol | 1 | Amazon | 2.50 |
+----+-----------------+-----------+-------------+-------+
(table d)
I.e. the first row is just the result of left outer join and the rest of the results are the the inner join.
An even more desired outcome is:
+------+-----------------+-----------+-------------+-------+
| pk | title | author_id | shop_name | price |
+------+-----------------+-----------+-------------+-------+
| 1 | The Lost Symbol | 1 | NULL | NULL |
| NULL | NULL | NULL | WHSmith | 5.00 |
| NULL | NULL | NULL | Waterstones | 7.00 |
| NULL | NULL | NULL | Amazon | 2.50 |
+------+-----------------+-----------+-------------+-------+
(table e)
Having shop_name and price concatenated and grouped in a single row seems not to work as it only does the first result from shops instead of all of them, also in my real world scenario, I have punctuation in the data so have to be careful with the separator.
So how would I get the result of table e?

You can use UNION ALL to build the required result set:
SELECT pk, title, author_id, NULL AS shop_name, NULL AS price
FROM books
WHERE books.title = "The Lost Symbol"
UNION ALL
SELECT NULL AS pk, NULL AS title, NULL AS author_id, shops.shop_name, shops.price
FROM books
JOIN shops ON books.pk = shops.book_id
WHERE books.title = "The Lost Symbol"
The first part of the union operation returns the first row of the result, i.e. the book title. The second part returns the rest of the rows, i.e.the shop names.
Demo here

Related

mysql pivot using column and row numbers

I am stuck in this situation where I need to use Row Number and Column Number values from table's columns to derive the output mentioned below. I have tried everything - if/else, case when/then but not helping.
Any help/suggestions are really appreciated!
Here is a mocked up sample data present in db table -
+--------+--------+--------+----------+-------------+
| Record | ColNbr | RowNbr | ColTitle | CellContent |
+--------+--------+--------+----------+-------------+
| 1 | 1 | 1 | Unit | sqf |
| 1 | 1 | 2 | Unit | cm |
| 1 | 2 | 1 | Desc | roof |
| 1 | 2 | 2 | Desc | rod |
| 1 | 3 | 1 | Material | concrete |
| 1 | 3 | 2 | Material | steel |
| 1 | 4 | 1 | Quantity | 100 |
| 1 | 4 | 2 | Quantity | 12 |
| 1 | 1 | 1 | Unit | liter |
| 1 | 1 | 2 | Unit | ml |
| 1 | 2 | 1 | Desc | bowl |
| 1 | 2 | 2 | Desc | plate |
| 1 | 3 | 1 | Material | plastic |
| 1 | 3 | 2 | Material | glass |
| 1 | 4 | 1 | Quantity | 2 |
| 1 | 4 | 2 | Quantity | 250 |
+--------+--------+--------+----------+-------------+
Expected Output -
+--------+--------+--------+----------+-------------+
| Record | Unit | Desc | Material | Quantity |
+--------+--------+--------+----------+-------------+
| 1 | sqf | roof | concrete | 100 |
| 1 | cm | rod | steel | 12 |
| 2 | liter | bowl | plastic | 2 |
| 2 | ml | plate | glass | 250 |
+--------+--------+--------+----------+-------------+
If your actual data is like that, I suggest that you consider to separate the data to; for example, 4 different tables (unit,description,material & a table to store all that ids+quantity). The former 3 tables will store the prerequisite info that get minor updates throughout time and the last table will store all the quantity records. Let's say your tables will look something like this:
CREATE TABLE `Unit` (
unit_id INT,
unit_name VARCHAR(50));
+---------+-----------+
| unit_id | unit_name |
+---------+-----------+
| 1 | sqf |
| 2 | cm |
| 3 | liter |
| 4 | ml |
+---------+-----------+
CREATE TABLE `Description` (
desc_id INT,
desc_name VARCHAR(50));
+---------+-----------+
| desc_id | desc_name |
+---------+-----------+
| 1 | roof |
| 2 | rod |
| 3 | bowl |
| 4 | plate |
+---------+-----------+
CREATE TABLE `Material` (
mat_id INT,
mat_name VARCHAR(50));
+--------+----------+
| mat_id | mat_name |
+--------+----------+
| 1 | concrete |
| 2 | steel |
| 3 | plastic |
| 4 | glass |
+--------+----------+
CREATE TABLE `Records` (
unit_id INT,
desc_id INT,
mat_id INT,
quantity DECIMAL(14,4));
+---------+---------+--------+----------+
| unit_id | desc_id | mat_id | Quantity |
+---------+---------+--------+----------+
| 1 | 1 | 1 | 100 |
| 2 | 2 | 2 | 12 |
| 3 | 3 | 3 | 2 |
| 4 | 4 | 4 | 250 |
+---------+---------+--------+----------+
Then you insert the data accordingly.
Anyhow, for your existing data example, it could be done but there are some concern over whether the unit+desc+material+quantity matching are correct. The only way I can maybe at least think that it's correctly matched is by giving all of the query a similar ORDER BY clause. Hence, the following:
SELECT A.record,A.unit,B.Desc,C.Material,D.Quantity FROM
(SELECT #rn:=#rn+1 AS record,CASE WHEN coltitle='unit' THEN cellcontent END AS Unit
FROM yourtable, (SELECT #rn :=0 ) v
HAVING unit IS NOT NULL
ORDER BY colnbr) A LEFT JOIN
(SELECT #rn1:=#rn1+1 AS record,CASE WHEN coltitle='Desc' THEN cellcontent END AS `Desc`
FROM yourtable, (SELECT #rn1 :=0 ) v
HAVING `Desc` IS NOT NULL
ORDER BY colnbr) B ON a.record=b.record LEFT JOIN
(SELECT #rn2:=#rn2+1 AS record,CASE WHEN coltitle='material' THEN cellcontent END AS Material
FROM yourtable, (SELECT #rn2:=0 ) v
HAVING Material IS NOT NULL
ORDER BY colnbr) C ON a.record=c.record LEFT JOIN
(SELECT #rn3:=#rn3+1 AS record,CASE WHEN coltitle='Quantity' THEN cellcontent END AS Quantity
FROM yourtable, (SELECT #rn3:=0 ) v
HAVING Quantity IS NOT NULL
ORDER BY colnbr) D ON a.record=d.record;
The idea here is to make a sub-query based on COLTITLE then assign a numbering/ranking (#rn,#rn1,#rn2,#rn3) variable to each of the sub-query and join them up using LEFT JOIN. Now, this experiment works to exactly return the output that you need but its not a definite answer because there are some part that is questionable especially on matching the combination correctly. Hopefully, this will give you some idea.

How to ROLLUP a SUM over multiple columns on separate tables?

I have 4 tables from which I want to aggregate data using MySQL 5.7.
Projects
+------------+--------+------------------+
| project_id | org_id | name |
+------------+--------+------------------+
| 1 | 1 | Big Project |
| 2 | 1 | Internal Project |
+------------+--------+------------------+
Tasks
+-----------+--------+----------------+------------+
| task_id | org_id | name | project_id |
+-----------+--------+----------------+------------+
| 1 | 1 | Check Work | 1 |
| 2 | 1 | Fix Code | 1 |
| 3 | 1 | Rebuild Office | 2 |
+-----------+--------+----------------+------------+
Resources
+-------------+--------+-------------+-----------+
| resource_id | org_id | first_name | last_name |
+-------------+--------+-------------+-----------+
| 1 | 1 | Alice | Black |
| 2 | 1 | Bob | Smith |
| 3 | 1 | Charlie | White |
+-------------+--------+-------------+-----------+
Task_Details
+-------------+--------+---------+-------------+
| resource_id | org_id | task_id | total_hours |
+-------------+--------+---------+-------------+
| 1 | 1 | 1 | 12 |
| 2 | 1 | 1 | 4 |
| 3 | 1 | 1 | 8 |
| 2 | 1 | 2 | 4 |
| 3 | 1 | 2 | 4 |
| 1 | 1 | 3 | 16 |
+-------------+--------+---------+-------------+
I want to SUM the total_hours, GROUPing by task and project, while still showing the total_hours each employee has individually spent on a task. The output I'm looking for would be something like this
Desired Output
+------------------+----------------+------------+-----------+-------------+
| project_name | task_name | first_name | last_name | total_hours |
+------------------+----------------+------------+-----------+-------------+
| Big Project | Check Work | Alice | Green | 12 |
| Big Project | Check Work | Bob | Smith | 4 |
| Big Project | Check Work | Charlie | Brown | 8 |
| Big Project | Check Work | NULL | NULL | 24 |
| Big Project | Fix Code | Bob | Smith | 4 |
| Big Project | Fix Code | Charlie | Brown | 4 |
| Big Project | Fix Code | NULL | NULL | 8 |
| Big Project | NULL | NULL | NULL | 32 |
| Internal Project | Rebuild Office | Alice | Green | 16 |
| Internal Project | Rebuild Office | NULL | NULL | 16 |
| Internal Project | NULL | NULL | NULL | 16 |
+------------------+----------------+------------+-----------+-------------+
I've managed to create a query that JOINs the relevant tables together, and even managed to GROUP them by project_id, task_id and resource_id. However, adding a WITH ROLLUP statement to the end of my query causes it to fail even though it works without one.
This is my current query:
SELECT
t1.project_name,
t1.task_name,
t2.first_name,
t2.last_name,
SUM(t1.task_hours)
FROM (
SELECT
Projects.project_id,
Projects.name AS project_name,
Tasks.task_id,
Tasks.name AS task_name,
Resources.resource_id,
Task_Details.total_hours AS task_hours
FROM
Projects
RIGHT OUTER JOIN
Tasks
ON
Projects.org_id = Tasks.org_id AND
Projects.project_id = Tasks.project_id
LEFT OUTER JOIN
Task_Details
ON
Task_Details.org_id = Tasks.org_id AND
Task_Details.task_id = Tasks.task_id
LEFT OUTER JOIN
Resources
ON
Resources.org_id = Task_Details.org_id AND
Resources.resource_id = Task_Details.resource_id
WHERE
Projects.org_id = 1
) AS t1
JOIN (
SELECT
resource_id,
first_name,
last_name
FROM
Resources
WHERE
org_id = 1
) AS t2
ON
t2.resource_id = t1.resource_id
GROUP BY
t1.project_id,
t1.task_id,
t1.resource_id;
How can I modify my query such that WITH ROLLUP works?
My SQLFiddle is here, but notably is for MySQL 5.6 rather than 5.7
IMHO, the problem with your query is this: You select some columns which are not in the GROUP BY. That causes some non-sensical values in the columns first_name, last_name, project_name and task_name. However, the sum column is probably correct, isn't it?
This works for me:
SELECT p.name as project_name,
s1.task_name,
first_name,
last_name,
s1.total_hours
FROM (
SELECT
t.project_id,
t.name as task_name,
h.resource_id,
sum(h.total_hours) as total_hours
FROM Task_Details as h
JOIN Tasks as t ON (t.task_id=h.task_id)
GROUP BY t.project_id, t.name, h.resource_id WITH ROLLUP
) AS s1
LEFT JOIN Resources AS r ON (s1.resource_id=r.resource_id)
JOIN Projects AS p ON (p.project_id=s1.project_id);
The nested SELECT does the interesting work, it sums up the total_hours of every resource_id, every task_name and and every project_id. The nesting SELECT then collects the name of every resource and project.
OUTPUT:
+------------------+----------------+------------+-----------+-------------+
| project_name | task_name | first_name | last_name | total_hours |
+------------------+----------------+------------+-----------+-------------+
| Big Project | NULL | NULL | NULL | 32 |
| Big Project | Check Work | Alice | Green | 12 |
| Big Project | Check Work | NULL | NULL | 24 |
| Big Project | Check Work | Bob | Smith | 4 |
| Big Project | Check Work | Charlie | Brown | 8 |
| Big Project | Fix Code | Bob | Smith | 4 |
| Big Project | Fix Code | Charlie | Brown | 4 |
| Big Project | Fix Code | NULL | NULL | 8 |
| Internal Project | NULL | NULL | NULL | 16 |
| Internal Project | Rebuild Office | NULL | NULL | 16 |
| Internal Project | Rebuild Office | Alice | Green | 16 |
+------------------+----------------+------------+-----------+-------------+
Hope this helps.

MySQL query with three tables, one self referenced and with same fields. What's the best approach?

I am trying to write a query to bring together data from three tables:
---------------------------------
| destinations |
---------------------------------
| id | city | status |
---------------------------------
| 1 | Milan | Open |
| 2 | Florence | Open |
| 3 | Venice | Open |
---------------------------------
---------------------------------
| trips |
---------------------------------
| id | from | to | train |
---------------------------------
| 1 | 1 | 2 | 2 |
| 2 | 1 | 2 | 3 |
| 3 | 2 | 1 | 2 |
| 4 | 2 | 3 | 2 |
| 5 | 1 | 3 | 1 |
| 6 | 3 | 1 | 1 |
---------------------------------
---------------------------------
| trains |
---------------------------------
| id | train |
---------------------------------
| 1 | T1 |
| 2 | ChooChoo |
| 3 | IC123 |
---------------------------------
The idea is that I would like to be able to show my user all the trips starting or ending in - say - Florence.
Something along these lines:
-----------------------------------------------------------------
| Query: all trains going to/from Florence |
-----------------------------------------------------------------
| trips.id | from.id | from (city) | to.id | to. city | train |
-----------------------------------------------------------------
| 1 | 1 | Milan | 2 | Florence | 2 |
| 2 | 1 | Milan | 2 | Florence | 3 |
| 3 | 2 | Florence | 1 | Milan | 2 |
| 4 | 2 | Florence | 3 | Venice | 2 |
-----------------------------------------------------------------
The problems that I'm facing are essentially two: the auto-referencing table of destinations (which I can easily solve with aliases) and the fact that I am trying to combine two sets of data from two different selects (which I thought of solving with temporary tables).
Now, it's all good if it wasn't that some columns have the same names. Because I want to preserve the "ids" in my temporary table (to be used with links) I can't create a temporary table with a wildcard (eg. SELECT * FROM), but I would have to spell out all the columns and write a monstrous query. It will work, but it won't be flexible, and if I'll add other columns in the future, it will be hell updating it!
Can any MySQL guru maybe suggest a better approach please?
Thanks and cheers from Australia.
G'day...
DROP TABLE IF EXISTS destinations;
CREATE TABLE destinations
(city_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,city VARCHAR(12) NOT NULL UNIQUE
,status VARCHAR(12) NOT NULL
);
INSERT INTO destinations VALUES
(1,'Milan','Open'),
(2,'Florence','Open'),
(3,'Venice','Open');
DROP TABLE IF EXISTS trips;
CREATE TABLE trips
(trip_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,from_city_id INT NOT NULL
,to_city_id INT NOT NULL
,train INT NOT NULL
,UNIQUE(from_city_id,to_city_id,train)
);
INSERT INTO trips VALUES
(1,1,2,2),
(2,1,2,3),
(3,2,1,2),
(4,2,3,2),
(5,1,3,1),
(6,3,1,1);
DROP TABLE IF EXISTS trains;
CREATE TABLE trains
(train_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,train VARCHAR(12) NOT NULL UNIQUE
);
INSERT INTO trains VALUES
(1,'T1'),
(2,'ChooChoo'),
(3,'IC123');
SELECT * FROM destinations;
+---------+----------+--------+
| city_id | city | status |
+---------+----------+--------+
| 1 | Milan | Open |
| 2 | Florence | Open |
| 3 | Venice | Open |
+---------+----------+--------+
SELECT * FROM trips;
+---------+--------------+------------+-------+
| trip_id | from_city_id | to_city_id | train |
+---------+--------------+------------+-------+
| 1 | 1 | 2 | 2 |
| 2 | 1 | 2 | 3 |
| 5 | 1 | 3 | 1 |
| 3 | 2 | 1 | 2 |
| 4 | 2 | 3 | 2 |
| 6 | 3 | 1 | 1 |
+---------+--------------+------------+-------+
SELECT * FROM trains;
+----------+----------+
| train_id | train |
+----------+----------+
| 2 | ChooChoo |
| 3 | IC123 |
| 1 | T1 |
+----------+----------+
SELECT t.trip_id
, t.from_city_id
, from_city.city
, t.to_city_id
, to_city.city
, t.train
FROM trips t
JOIN destinations from_city
ON from_city.city_id = t.from_city_id
JOIN destinations to_city
ON to_city.city_id = t.to_city_id
WHERE 'Florence' IN(from_city.city,to_city.city);
+---------+--------------+----------+------------+----------+-------+
| trip_id | from_city_id | city | to_city_id | city | train |
+---------+--------------+----------+------------+----------+-------+
| 3 | 2 | Florence | 1 | Milan | 2 |
| 4 | 2 | Florence | 3 | Venice | 2 |
| 1 | 1 | Milan | 2 | Florence | 2 |
| 2 | 1 | Milan | 2 | Florence | 3 |
+---------+--------------+----------+------------+----------+-------+
when you build a report, usually isn't a good idea to use SELECT *,
because when i modify a table, the report can shows wrong result.
usually i prefere to write the id field with id_content, ex. id_destination, id_to, id_train, ecc...
SELECT trips.id, f.id, f.city, t.id, t. city, trains.train
FROM trips trips
INNER JOIN destinations f
ON trips.from = f.id
INNER JOIN destinations t
ON trips.from = t.id
INNER JOIN trains trains
ON trips.train = trains.id
ORDER BY 1 ASC

How select remaining unspecified columns

I am looking to overwrite a column name in a table with an existing column name.
I am Looking for a way to get the remaining unspecified columns in the tables.
Note:
The query could have more joins in the future.
eg
Person
+-----------+----------+---------+
| firstname | lastname | pers_id |
+-----------+----------+---------+
| Joe | Soap | 1 |
| Bobby | Pin | 2 |
| Janet | Jackson | 3 |
+-----------+----------+---------+
Category
+----------+-------------------+--------+
| type | description | cat_id |
+----------+-------------------+--------+
| customer | people who pay us | 1 |
| employee | people we pay | 2 |
| director | people who direct | 3 |
+----------+-------------------+--------+
Person_Cat
(=^ェ^=)
+---------+--------+
| pers_id | cat_id |
+---------+--------+
| 3 | 1 |
| 2 | 2 |
| 1 | 3 |
+---------+--------+
Query
SELECT *, CONCAT(p.firstname, ' '
, p.lastname) as full_name
, c.cat_id AS category_id
, p.pers_id AS cat_id
FROM Person AS p
JOIN Person_Cat AS pc ON(p.pers_id = pc.pers_id)
JOIN Category AS c ON (pc.cat_id = c.cat_id)
OUTPUT
(Apologies for the length but the table after is more important)
+-----------+----------+---------+---------+--------+----------+-------------------+--------+---------------+-------------+--------+
| p | p | p | pc | pc | c | c | c | Select | Select | Select |
+-----------+----------+---------+---------+--------+----------+-------------------+--------+---------------+-------------+--------+
| firstname | lastname | pers_id | pers_id | cat_id | type | description | cat_id | full_name | category_id | cat_id |
+-----------+----------+---------+---------+--------+----------+-------------------+--------+---------------+-------------+--------+
| Janet | Jackson | 3 | 3 | 1 | customer | people who pay us | 1 | Janet jackson | 1 | 3 |
| Bobby | Pin | 2 | 2 | 2 | employee | people who we pay | 2 | Bobby Pin | 2 | 2 |
| Joe | Soap | 1 | 1 | 3 | director | people who direct | 3 | Joe Soap | 3 | 1 |
+-----------+----------+---------+---------+--------+----------+-------------------+--------+---------------+-------------+--------+
The headers above column names are there for reference
to where they comes from.
Column summary -
firstname, lastname, pers_id, pers_id, cat_id, type,
description, cat_id, full_name ,category_id, cat_id
Wanted output
+-----------+----------+---------+--------+----------+-------------------+---------------+-------------+--------+
| p | p | pc | pc | c | c | Select | Select | Select |
+-----------+----------+---------+--------+----------+-------------------+---------------+-------------+--------+
| firstname | lastname | pers_id | cat_id | type | description | full_name | category_id | cat_id |
+-----------+----------+---------+--------+----------+-------------------+---------------+-------------+--------+
| Janet | Jackson | 3 | 1 | customer | people who pay us | Janet jackson | 1 | 3 |
| Bobby | Pin | 2 | 2 | employee | people who we pay | Bobby Pin | 2 | 2 |
| Joe | Soap | 1 | 3 | director | people who direct | Joe Soap | 3 | 1 |
+-----------+----------+---------+--------+----------+-------------------+---------------+-------------+--------+
Column summary -
firstname, lastname, pers_id, cat_id, type,
description, full_name ,category_id, cat_id
Notice:
The p.pers_id and the c.cat_id are not present. I would like to think this would be because the were called directly and unmodified unlike the first and lastname used in ConCat
When the short answer is that there is no such concept as Select [remaining columns]at this time (2015-06-17), if you want to use SELECT * but only remove redundant columns,
then you will need to explicitly remove (ignore) those redundant columns when rendering your view.
You will have to explicitly configure logic of which columns to ignore, which is pretty much the same thing as explicitly listing the columns that you are interested in, so you get back to the argument against selecting all columns that I made in the comments above.
Unless your table schema is changing all the time, there really isn't reason for this.

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