MYSQL多表导出到平板csv

时间:2014-08-27 06:22:23

标签: mysql sql

我试图从Joomla配方插件中导出数据,该插件已不再维护,并且在更复杂的部分出现问题。

我一直在努力研究这篇文章,其中详细介绍了一个与我尝试实现的过程类似的过程:http://greenash.net.au/thoughts/2012/05/flattening-many-to-many-fields-for-mysql-to-csv-export/

以下是我正在使用的表格:

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天后,我无法理解这一点。 :(

非常感谢任何帮助或指导。

1 个答案:

答案 0 :(得分:0)

好的,我抓住了这个并想出了一个解决方案。一旦我理解了使用子查询的概念,它实际上是相当直接的。

我头脑中的灯泡是:

每个子查询基本上都成为它在查询中使用的临时表,由它的别名引用。换句话说,我们在每个子查询中的表之间创建链接,然后在SELECT语句中使用它们的输出数据,就像它是普通表一样。

如果有人对下面的最终查询感兴趣,则由于使用不同的数据集而命名略有不同的albiet。

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

我只是使用多个连接的子查询来将内容过滤到我需要的内容,然后针对我想要的每个类别重复。

希望这有助于其他人。

相关问题