[英]MYSQL multi table export to flat csv
我正在尝试从不再维护的Joomla食谱插件中导出数据,并且遇到了更复杂的部分的问题。
我一直在尝试研究这篇文章,其中详细介绍了一个与我要实现的过程类似的过程: http : //greenash.net.au/thoughts/2012/05/flattening-many-to-many- mysql到csv导出的字段/
这是我正在使用的表:
jos_rr_recipes
+-----------+------+-------------+-----------------+---------------+------------+ | recipe_id | chef | title | introtext | portionstype | portions | +-----------+------+-------------+-----------------+---------------+------------+ | 3 | 1 | Recipe ID 3 | Intro text ID 3 | 2 | 7-8 People | | 6 | 1 | Recipe ID 6 | Intro text ID 6 | 2 | 3-4 People | | 4 | 3 | Recipe ID 4 | Intro text ID 4 | 1 | 1 box | | 5 | 3 | Recipe ID 5 | Intro text ID 5 | 1 | 10 wraps | +-----------+------+-------------+-----------------+---------------+------------+
jos_rr_categories
+-------------+------------+-----------+-----------+ | category_id | pretty_url | title | parent_id | +-------------+------------+-----------+-----------+ | 3 | 3-url | 3 Title | 0 | | 198 | 198-url | 198 Title | 0 | | 2 | 2-url | 2 Title | 0 | | 5 | 5-url | 5 Title | 0 | | 1 | 1-url | 1 Title | 0 | | 169 | 169-url | 169 Title | 0 | | 171 | 171-url | 171 Title | 0 | | 149 | 149-url | 149 Title | 3 | | 150 | 150--url | 150 Title | 3 | | 151 | 151-url | 151 Title | 3 | | 198 | 98-url | 98 Title | 3 | | 201 | 201-url | 201 Title | 198 | | 21 | 21--url | 21 Title | 198 | | 100 | 100-url | 100 Title | 198 | | 4 | 4-url | 4 Title | 169 | | 80 | 80-url | 80 Title | 169 | | 26 | 26-url | 26 Title | 2 | | 213 | 213-url | 213 Title | 198 | | 303 | 303-url | 303 Title | 171 | | 11 | 11-url | 11 Title | 2 | | 112 | 112-url | 112 Title | 2 | | 231 | 231-url | 231 Title | 1 | | 200 | 200-url | 200 Title | 1 | | 181 | 181-url | 181 Title | 1 | | 54 | 54-url | 54 Title | 3 | | 195 | 195-url | 195 Title | 198 | | 10 | 10-url | 10 Title | 198 | | 226 | 226-url | 226 Title | 150 | | 300 | 300-url | 300 Title | 150 | +-------------+------------+-----------+-----------+
jos_rr_recipecategory
+-----------+-------------+ | recipe_id | category_id | +-----------+-------------+ | 3 | 195 | | 3 | 10 | | 3 | 149 | | 3 | 201 | | 3 | 26 | | 3 | 231 | | 3 | 80 | | 3 | 303 | | 4 | 54 | | 4 | 300 | | 4 | 4 | | 4 | 21 | | 4 | 98 | | 4 | 26 | | 4 | 213 | | 5 | 26 | | 5 | 11 | | 5 | 112 | | 5 | 200 | | 6 | 201 | | 6 | 4 | | 6 | 26 | | 6 | 112 | | 6 | 231 | | 6 | 300 | +-----------+-------------+
jos_sobi2_fields_data
+----+---------+--------------------+--------+ | id | fieldid | data_txt | itemid | +----+---------+--------------------+--------+ | 4 | 7 | chef1@email.com | 1 | | 5 | 13 | Description Chef 1 | 1 | | 28 | 7 | chef3@email.com | 3 | | 32 | 13 | description chef 3 | 3 | +----+---------+--------------------+--------+
其中一些类别是多层的,但是无法将其输入到我知道的WordPress中,因此我很乐意将它们作为group_concat放在单独的列中,在每个主标题下的输出中。
我将在WordPress中将其导入各自的分类法中的父母:
Course/Dish = 105 Meals = 199 Main Ingredient = 2 Difficulty = 5 Cuisine = 1 Equipment = 169 Rainbow = 171
这是我到目前为止的内容:
SELECT
recipes.recipe_id AS ID,
recipes.title AS Title,
chef.data_txt AS Chef,
recipes.introtext AS Description,
IFNULL(IF (recipes.portionstype = 1 , recipes.portions,''),'') AS Yield,
IFNULL(IF (recipes.portionstype = 2 , recipes.portions,''),'') AS Servings
FROM
jos_rr_recipes AS recipes
JOIN jos_sobi2_fields_data AS chef
ON recipes.chef = chef.itemid AND chef.fieldid = 7
GROUP BY recipes.recipe_id
现在,我不知道如何将每个分类法放入自己的列中。 我尝试了以下操作,但未成功:
SELECT
recipes.recipe_id AS ID,
recipes.title AS Title,
chef.data_txt AS Chef,
recipes.introtext AS Description,
IFNULL(IF (recipes.portionstype = 1 , recipes.portions,''),'') AS Yield,
IFNULL(IF (recipes.portionstype = 2 , recipes.portions,''),'') AS Servings,
IFNULL(tax_ingr.ingredients,'') AS "Taxonomy Ingredients"
FROM (
SELECT
GROUP_CONCAT(cat.title SEPARATOR '|') AS ingredients
FROM jos_rr_recipecategory AS catlink
LEFT JOIN jos_rr_categories AS cat
ON catlink.category_id = cat.category_id AND cat.parent_id = 2
GROUP BY recipe_id
) AS tax_ingr,
jos_rr_recipes AS recipes
JOIN jos_sobi2_fields_data AS chef
ON recipes.chef = chef.itemid AND chef.fieldid = 7
GROUP BY recipes.recipe_id
查询时间很长,无法提供预期的结果。
3天后,我无法解决这个问题。 :(
任何帮助或指导将不胜感激。
好的,我对此有所了解,并提出了解决方案。 一旦我了解了使用子查询的概念,它实际上是相当简单的。
我头上掉下来的灯泡是:
每个子查询实际上都变成了它自己的临时表,该表将在查询中使用,由其别名引用。 换句话说,我们在每个子查询中的表之间创建链接,然后在SELECT语句中使用它们的输出数据,就好像它是普通表一样。
如果有人对下面的最终查询感兴趣,则由于使用了不同的数据集,所以命名稍有不同。
SELECT
recipe.recipe_id AS ID,
recipe.title AS Title,
LOWER(IFNULL(recipe.chef,'nochef@aaronportbury.com')) AS Chef,
recipe.description AS Description,
recipe.yield,
recipe.servings,
IFNULL(ingr_link.val,'') AS 'Ingredients',
IFNULL(course_link.val,'') AS 'Course',
IFNULL(meal_link.val,'') AS 'Meal',
IFNULL(dif_link.val,'') AS 'Difficulty',
IFNULL(cuis_link.val,'') AS 'Cuisine',
IFNULL(equi_link.val,'') AS 'Equipment',
FROM (
SELECT
r.recipe_id,
r.title,
c.data_txt AS chef,
r.introtext AS description,
IFNULL(IF (r.portionstype = 1 , r.portions,''),'') AS yield, # Checks to see type from Portionstype. If 1, then it's a Yield. Else leave empty
IFNULL(IF (r.portionstype = 2 , r.portions,''),'') AS servings, # Checks to see type from Portionstype. If 2, then it's a Serving. Else leave empty
FROM jos_rr_recipes AS r
LEFT JOIN jos_sobi2_fields_data AS c
ON r.chef = c.itemid AND c.fieldid = 7
) AS recipe
LEFT JOIN (
SELECT
rc.recipe_id AS rid,
IFNULL(GROUP_CONCAT(cat.title SEPARATOR ';;'),'') AS val # Uses Group_Concat function to combine all Ingredients related to the recipe ID and seperates them with ;;
FROM jos_rr_recipecategory AS rc
LEFT JOIN jos_rr_categories AS cat
ON rc.category_id = cat.category_id AND cat.parent_id = 2
GROUP BY rc.recipe_id
) AS ingr_link
ON recipe.recipe_id = ingr_link.rid
LEFT JOIN (
SELECT
rc.recipe_id AS rid,
IFNULL(GROUP_CONCAT(cat.title SEPARATOR ';;'),'') AS val
FROM jos_rr_recipecategory AS rc
LEFT JOIN jos_rr_categories AS cat
ON rc.category_id = cat.category_id
AND (cat.parent_id = 3
OR cat.parent_id = 23
OR cat.parent_id = 26
OR cat.parent_id = 28
OR cat.parent_id = 29
OR cat.parent_id = 30
OR cat.parent_id = 59
OR cat.parent_id = 211) # Here I just found all the parent ids and created multiple OR tests to collate them.
GROUP BY rc.recipe_id
) AS course_link
ON recipe.recipe_id = course_link.rid
LEFT JOIN (
SELECT
rc.recipe_id AS rid,
IFNULL(GROUP_CONCAT(cat.title SEPARATOR ';;'),'') AS val
FROM jos_rr_recipecategory AS rc
LEFT JOIN jos_rr_categories AS cat
ON rc.category_id = cat.category_id AND cat.parent_id = 198
GROUP BY rc.recipe_id
) AS meal_link
ON recipe.recipe_id = meal_link.rid
LEFT JOIN (
SELECT
rc.recipe_id AS rid,
IFNULL(GROUP_CONCAT(cat.title SEPARATOR ';;'),'') AS val
FROM jos_rr_recipecategory AS rc
LEFT JOIN jos_rr_categories AS cat
ON rc.category_id = cat.category_id AND cat.parent_id = 5
GROUP BY rc.recipe_id
) AS dif_link
ON recipe.recipe_id = dif_link.rid
LEFT JOIN (
SELECT
rc.recipe_id AS rid,
IFNULL(GROUP_CONCAT(cat.title SEPARATOR ';;'),'') AS val
FROM jos_rr_recipecategory AS rc
LEFT JOIN jos_rr_categories AS cat
ON rc.category_id = cat.category_id AND cat.parent_id = 1
GROUP BY rc.recipe_id
) AS cuis_link
ON recipe.recipe_id = cuis_link.rid
LEFT JOIN (
SELECT
rc.recipe_id AS rid,
IFNULL(GROUP_CONCAT(cat.title SEPARATOR ';;'),'') AS val
FROM jos_rr_recipecategory AS rc
LEFT JOIN jos_rr_categories AS cat
ON rc.category_id = cat.category_id AND cat.parent_id = 169
GROUP BY rc.recipe_id
) AS equi_link
ON recipe.recipe_id = equi_link.rid
GROUP BY recipe.recipe_id
我只是简单地使用多个联接的子查询来将内容过滤到所需的内容,然后针对所需的每个类别重复进行。
希望这对其他人有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.