ANSWERED -- MySQL - Function SELECT in a SELECT

Louis Margot :

For my problem, I don't know how to use the SELECT function in another SELECT (with MySQL).

Alone, the SELECT works (all parts of the code) great but when I put the full code, PHPMyAdmin says 'Subquery returns more than 1 row'.

The full code is :

INSERT INTO `vente` (`Quantite`, `Année`, `Mois`, `Jour`, `ID_TypeEquipement`, `ID_Divinite`, `ID_Pouvoir`, `ID_Lieu`, `ID_DemiDieu`)
    SELECT `_info_ventes_1er_siecle`.`Quantité`, `_info_ventes_1er_siecle`.`Année`, `_info_ventes_1er_siecle`.`Mois`, `_info_ventes_1er_siecle`.`Jour`
        , (SELECT `ID`
            FROM `typeequipement`
            INNER JOIN `_info_ventes_1er_siecle`
            ON `_info_ventes_1er_siecle`.`Objet` = `typeequipement`.`Denomination`)
        , (SELECT `ID_Divinite`
            FROM `mois`
            INNER JOIN `_info_ventes_1er_siecle`
            ON `_info_ventes_1er_siecle`.`Mois` = `mois`.`Nom`)
        , (SELECT `ID`
            FROM `pouvoir`
            INNER JOIN `_info_ventes_1er_siecle`
            ON `_info_ventes_1er_siecle`.`Pouvoir` = `pouvoir`.`Denomination`)
        , (SELECT `ID_Lieu`
            FROM `lieu`
            INNER JOIN `_info_ventes_1er_siecle`
            ON `_info_ventes_1er_siecle`.`Province` = `lieu`.`Province`
            AND `_info_ventes_1er_siecle`.`Ville` = `lieu`.`Ville`)
        , (SELECT `ID_Lieu`
            FROM `lieu`
            INNER JOIN `_info_ventes_1er_siecle`
            ON `_info_ventes_1er_siecle`.`Ville` = `lieu`.`Ville`)
    FROM `_info_ventes_1er_siecle`;

It works when I try this :

SELECT `ID`
    FROM `typeequipement`
    INNER JOIN `_info_ventes_1er_siecle`
    ON `_info_ventes_1er_siecle`.`Objet` = `typeequipement`.`Denomination`

Or this :

SELECT `_info_ventes_1er_siecle`.`Quantité`, `_info_ventes_1er_siecle`.`Année`, `_info_ventes_1er_siecle`.`Mois`, `_info_ventes_1er_siecle`.`Jour`

So the error is just when I add the ", (SELECT ...)" parts. It's probably the syntax. To be honest, I don't know, it's why I'm here.


Thank you, I hope I can fix this error before the deadline. ;)

Bill Karwin :

The error means you are using scalar subqueries that must return 1 row and 1 column in the context you use them, but they in fact don't guarantee that they will return 1 row.

I guess that you want your SELECT to return the columns from related tables, where the value in the related table corresponds to the row from the outer query. But the way you have written it, that won't happen. You are creating a JOIN to a different table reference to _info_ventes_1er_siecle within each subquery. So it will return many values from the subquery, for any matching row, not just for the one corresponding to the row from the outer table. Just naming the same table does not make it the same table reference.

The solution using LIMIT won't work either, in general. It won't return the row from the subquery that corresponds to the row in the outer query. It will only return 1 arbitrary row from the subquery.

But it may not be necessary to use subqueries at all. I would use JOIN instead:

INSERT INTO `vente` (`Quantite`, `Année`, `Mois`, `Jour`, `ID_TypeEquipement`,
 `ID_Divinite`, `ID_Pouvoir`, `ID_Lieu`, `ID_DemiDieu`)
    SELECT 
      i.`Quantité`,
      i.`Année`,
      i.`Mois`,
      i.`Jour`
      t.`ID`,
      m.`ID_Divinite`,
      p.`ID`,
      l2.`ID_Lieu`,
      l1.`ID_Lieu`
    FROM `_info_ventes_1er_siecle` AS i
    LEFT JOIN `typeequipement` AS t ON i.`Objet` = t.`Denomination`
    LEFT JOIN `mois` AS m ON i.`Mois` = m.`Nom`
    LEFT JOIN `pouvoir` AS p ON i.`Pouvoir` = p.`Denomination`
    LEFT JOIN `lieu` AS l1 ON i.`Ville` = l1.`Ville`
    LEFT JOIN `lieu` AS l2 ON i.`Ville` = l2.`Ville` AND i.`Province` = l2.`Province`

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=402276&siteId=1