SELECT donnee.id_enregistrement, ',@col_createur, @col_date_creation, @col_date_derniere_modification, @COLUMNS
FROM ta_donnee_champ donnee
在变量@COLUMNS中有一个id列表,我在表格中搜索" ta_donnee_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)
有人可以给我一个简单的解释吗?
********* EDIT **************** ****************
在此过程中,@ 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。我如何才能最好地搜索ta_donnee_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;