1

JOINサブクエリとs を使用した複雑な MySQL クエリに取り組んでおり、これが私のクエリです。

SELECT
    id,
    ancient_source_name,
    ancient_source_name_alt,
    ancient_source_type,
    ancient_source_type_id,
    IFNULL(IF(ancient_source_material = 'Unknown', NULL, ancient_source_material), '-') AS ancient_source_material,
    ancient_source_year,
    dynasty,
    ancient_period,
    author,
    IFNULL(CONCAT_WS(', ',
        IF(provenance_country = 'Unknown' OR provenance_country = 'Not Applicable', NULL, provenance_country),
        IF(provenance_locality = 'Unknown' OR provenance_locality = 'Not Applicable', NULL, provenance_locality),
        IF(provenance_place = 'Unknown' OR provenance_place = 'Not Applicable', NULL, provenance_place)
    ), '-') AS provenance_loc,
    IFNULL(CONCAT_WS(', ',
        IF(current_country = 'Unknown' OR current_country = 'Not Applicable', NULL, current_country),
        IF(current_locality = 'Unknown' OR current_locality = 'Not Applicable', NULL, current_locality),
        IF(current_place = 'Unknown' OR current_place = 'Not Applicable', NULL, current_place)
    ), '-') AS current_loc
FROM (
    SELECT
        hgn_ancient_sources.id AS id,
        ancient_source_name,
        IFNULL(ancient_source_name_alt, '-') AS ancient_source_name_alt,
        IFNULL(ancient_source_year, '-') AS ancient_source_year,
        ancient_source_type,
        ancient_source_type_id,
        ancient_source_material,
        dynasty,
        ancient_period,
        provenance_countries.country AS provenance_country,
        provenance_localities.locality AS provenance_locality,
        provenance_places.place AS provenance_place,
        current_countries.country AS current_country,
        current_localities.locality AS current_locality,
        current_places.place AS current_place,
        IFNULL(author_name, '-') AS author
    FROM hgn_ancient_sources
    JOIN hgn_ancient_source_types ON hgn_ancient_sources.ancient_source_type_id = hgn_ancient_source_types.id
    JOIN hgn_ancient_source_materials ON hgn_ancient_sources.ancient_source_material_id = hgn_ancient_source_materials.id
    JOIN hgn_dynasties ON hgn_ancient_sources.ancient_source_dynasty_id = hgn_dynasties.id
    JOIN hgn_ancient_periods ON hgn_ancient_sources.ancient_source_period_id = hgn_ancient_periods.id
    LEFT JOIN junc_ancient_source_has_author ON hgn_ancient_sources.id = junc_ancient_source_has_author.ancient_source_id
    LEFT JOIN hgn_authors ON junc_ancient_source_has_author.author_id = hgn_authors.id
    JOIN junc_place_has_location AS provenance_place_has_location ON hgn_ancient_sources.ancient_source_provenance_place_id = provenance_place_has_location.id
    JOIN junc_place_has_location AS current_place_has_location ON hgn_ancient_sources.ancient_source_current_place_id = current_place_has_location.id
    JOIN junc_locality_has_country AS provenance_locality_has_country ON provenance_place_has_location.location_id = provenance_locality_has_country.id
    JOIN junc_locality_has_country AS current_locality_has_country ON current_place_has_location.location_id = current_locality_has_country.id
    JOIN hgn_places AS provenance_places ON provenance_place_has_location.place_id = provenance_places.id
    JOIN hgn_places AS current_places ON current_place_has_location.place_id = current_places.id
    JOIN hgn_localities AS provenance_localities ON provenance_locality_has_country.locality_id = provenance_localities.id
    JOIN hgn_localities AS current_localities ON current_locality_has_country.locality_id = current_localities.id
    JOIN hgn_countries AS provenance_countries ON provenance_locality_has_country.country_id = provenance_countries.id
    JOIN hgn_countries AS current_countries ON current_locality_has_country.country_id = current_countries.id
) AS subquery

コードの次の部分を除いて、すべて正常に動作します。

IFNULL(CONCAT_WS(', ',
    IF(provenance_country = 'Unknown' OR provenance_country = 'Not Applicable', NULL, provenance_country),
    IF(provenance_locality = 'Unknown' OR provenance_locality = 'Not Applicable', NULL, provenance_locality),
    IF(provenance_place = 'Unknown' OR provenance_place = 'Not Applicable', NULL, provenance_place)
), '-') AS provenance_loc,
IFNULL(CONCAT_WS(', ',
    IF(current_country = 'Unknown' OR current_country = 'Not Applicable', NULL, current_country),
    IF(current_locality = 'Unknown' OR current_locality = 'Not Applicable', NULL, current_locality),
    IF(current_place = 'Unknown' OR current_place = 'Not Applicable', NULL, current_place)
), '-') AS current_loc

Unknownまたはの値を持つ1/3または 2/3 の列がある場合は良い結果が得られますが、Not Applicable3/3 の列 (たとえばcurrent_country、期待されるべきです。current_localitycurrent_place-

コードでさまざまな変更を試みましたが、何も機能しません。たとえば、クエリの次の部分について言及する必要があります。

IFNULL(IF(ancient_source_material = 'Unknown', NULL, ancient_source_material), '-') AS ancient_source_material

完全に機能するので、機能に問題があるとCONCAT_WS()思いますが、何が見つかりません。

4

1 に答える 1