如何将id列表放入带有sql变量的内部连接中?

c2e8gylq  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(137)

我事先道歉,我不擅长sql。我想在sql过程中做一些非常简单的事情,但我被卡住了: SELECT donnee.id_enregistrement, ',@col_createur, @col_date_creation, @col_date_derniere_modification, @COLUMNS FROM ta_donnee_champ donnee 在变量@columns中有一个id列表,我在表“tau donnee\u champ donnee”中搜索它。
我还想使用相同的id列表在同一查询中的另一个表中进行搜索。
我怎样才能把它放在一个内部连接中呢?
就像这样: SELECT donnee.id_enregistrement, ',@col_createur, @col_date_creation, @col_date_derniere_modification, @COLUMNS,' FROM ta_donnee_champ donnee INNER JOIN ta_champ_tableau ct ON ct.id_champ_colonne IN (@COLUMNS) 有人能给我一个简单的解释吗?

编辑**

在此过程中,@columns表示所需结果的列。对于这些列,我需要添加值,以下代码起作用:

IF @COLUMNS IS NOT NULL THEN
SET @SQL = CONCAT(
       'SELECT donnee.id_enregistrement, ',@col_createur, @col_date_creation, @col_date_derniere_modification, @COLUMNS,'
       FROM ta_donnee_champ donnee
       INNER JOIN t_enregistrement enregistrement
       ON donnee.id_enregistrement = enregistrement.id_enregistrement
       WHERE donnee.id_enregistrement IN (',IFNULL(p_id_enregistrement, 'donnee.id_enregistrement'),')
       AND donnee.id_enregistrement in (
           select DISTINCT id_enregistrement from ta_participant
           where id_groupe = ', p_id_groupe,'
       or id_groupe in (
           select id_groupe_lu
       from ta_droits_groupe
       inner join t_groupe on ta_droits_groupe.id_groupe_lu = t_groupe.id_groupe
       where id_groupe_lecteur = ', p_id_groupe,'
       and t_groupe.id_application = ', p_id_application,'
     )

    )
       GROUP BY donnee.id_enregistrement'
);

-- Prépare et exécute la requête
PREPARE stmt FROM @SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

我的问题是:我需要添加另一个表,从中我必须知道id在里面。我怎样才能最好地不仅在tau donnee\u champ donnee中搜索id,而且在另一个表中搜索id?
@columns是如何设置的:

SET @COLUMNS = NULL;

IF p_autoriser_tableau = 1 THEN
  SET @filtre = 'SELECT DISTINCT id_champ_colonne FROM ta_champ_tableau';
ELSE
  SET @filtre = 'SELECT DISTINCT id_champ_tableau AS id_champ FROM    ta_champ_tableau UNION SELECT DISTINCT id_champ_colonne FROM ta_champ_tableau';
END IF;

-- Construit les colonnes à  horizontaliser
SET @SQL1 = CONCAT('
SELECT GROUP_CONCAT(
    DISTINCT CONCAT(
           \'GROUP_CONCAT(IF(donnee.id_champ = \',
           champ.id_champ ,
           \', donnee.valeur, NULL)) AS `\',
           champ.id_champ,\'`\'
    )ORDER BY onglet.ordre, section.ordre, champ.ordre
) INTO @COLUMNS
FROM t_champ champ
INNER JOIN t_section section
ON section.id_section = champ.id_section
INNER JOIN t_onglet onglet
ON onglet.id_onglet = section.id_onglet
INNER JOIN ta_droits_champ droits_champ
ON droits_champ.id_groupe IN (
SELECT id_groupe FROM ta_agent_groupe WHERE id_agent = ', p_id_agent,'
)
AND droits_champ.id_champ = champ.id_champ
where onglet.id_application IN (', p_id_application,')
AND droits_champ.voir = 1
AND champ.id_champ NOT IN (',@filtre,')
AND
(
champ.date_fin_validite IS NULL
OR
date_format(champ.date_fin_validite, "%Y-%m-%d") > COALESCE(str_to_date("', p_date_validite,'", "%d/%m/%Y"), "01/01/1900")
)
;');

PREPARE stmt FROM @SQL1;

EXECUTE stmt;
DEALLOCATE PREPARE stmt;

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题