Boucle et résultats comparés de 2 tables

Résolu/Fermé
Sinistrus Messages postés 1017 Date d'inscription mercredi 12 décembre 2007 Statut Membre Dernière intervention 6 juin 2023 - Modifié le 23 sept. 2018 à 00:43
Sinistrus Messages postés 1017 Date d'inscription mercredi 12 décembre 2007 Statut Membre Dernière intervention 6 juin 2023 - 23 sept. 2018 à 17:33
Bonjour à tous !

Voilà, je rencontre une difficulté dans le résultat de cette requête.
Il double l'affichage et je n'arrives pas à corriger.

Voilà ce que j'essaie de faire et espère un coup de main.

tb_user_notifications(NumId, Date, Sujet)
tb_user_notifications_recap(NIC_Handle, Notification)

tb_user_notifications_recap.Notification = tb_user_notifications.NumId

Voici mes tables :
tb_user_notifications
NumId	Date			Sujet		Message
1	08/03/2017 19:07	Sujet 1		Texte de notification 1
2	15/03/2018 10:34	Sujet 2		Texte de notification 2
3	17/03/2018 16:59	Sujet 3		Texte de notification 3
4	22/09/2018 06:21	Sujet 4		Texte de notification 4
5	22/09/2018 08:33	sujet 5		Texte de notification 5
6	22/09/2018 17:29	Sujet 6		Texte de notification 6

tb_user_notifications_recap
NumId	NIC_Handle	Notification
1	JA90717		1
2	JA90717		2
3	JS38579		1

Alors, au lieu d'avoir:
22-09-2018 - Sujet 6 - Non lu
22-09-2018 - sujet 5 - Non lu
22-09-2018 - Sujet 4 - Non lu
17-03-2018 - Sujet 3 - Non lu
15-03-2018 - Sujet 2 - Lu
08-03-2017 - Sujet 1 - Lu

J'ai plutôt :
22-09-2018 - Sujet 6 - Non lu
22-09-2018 - Sujet 6 - Non lu
22-09-2018 - sujet 5 - Non lu
22-09-2018 - sujet 5 - Non lu
22-09-2018 - Sujet 4 - Non lu
22-09-2018 - Sujet 4 - Non lu
17-03-2018 - Sujet 3 - Non lu
17-03-2018 - Sujet 3 - Non lu
15-03-2018 - Sujet 2 - Non lu
15-03-2018 - Sujet 2 - Lu
08-03-2017 - Sujet 1 - Lu
08-03-2017 - Sujet 1 - Non lu

J'ai essayé ceci :
SELECT * FROM tb_user_notifications WHERE NumId IN (SELECT Notification FROM tb_user_notifications_recap WHERE NIC_Handle='".$_SESSION_NICHANDLE."' )

Mais je n'ai que comme résultat :
15-03-2018 - Sujet 2 - Lu
08-03-2017 - Sujet 1 - Lu

Je brûle là !

Voici mon code :
<?php try{$MyPDO=$pdo->query("SELECT * FROM tb_user_notifications ORDER BY Date DESC "); while($data=$MyPDO->fetch(PDO::FETCH_ASSOC)){ ?>

<?php try{$MyPDO_00 = $pdo->query("SELECT * FROM tb_user_notifications_recap WHERE NIC_Handle='".$_SESSION_NICHANDLE."'");
   while($data_00 = $MyPDO_00->fetch(PDO::FETCH_ASSOC)) { ?>
<?php if($data_00["Notification"] == $data["NumId"]) {$ICO = "fa fa-check";} else {$ICO = "fa fa-eye";} ?>

    <tr>
      <td><div style="padding:5px"><?php echo date("$DATE_SQL", strtotime($data["Date"])); ?></div></td>
      <td><div style="padding:5px"><?php echo $data["Sujet"]; ?></div></td>
      <td align="center"><button class="btn"><i class="<?php echo $ICO; ?>"></i></button></td>
    </tr>

<?php }}catch(PDOException $e){echo "<div class='alert alert-danger'>".$e->getMessage()."</div>";} ?>
<?php }}catch(PDOException $e){echo "<div class='alert alert-danger'>".$e->getMessage()."</div>";} ?>


J'espère avoir été assez explicite... merci de votre aide !

A voir également:

3 réponses

DelNC Messages postés 2234 Date d'inscription samedi 25 octobre 2014 Statut Membre Dernière intervention 22 février 2020 1 999
23 sept. 2018 à 02:39
Bonjour

Essaye de faire une requête avec Distinct

Voilà des exemple sur ce lien
https://www.w3schools.com/sql/sql_distinct.asp
2
jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 4 649
23 sept. 2018 à 07:38
Bonjour
Utilises donc un
LEFT JOIN
1
Sinistrus Messages postés 1017 Date d'inscription mercredi 12 décembre 2007 Statut Membre Dernière intervention 6 juin 2023 17
23 sept. 2018 à 15:11
Bonjour,
SELECT * 
FROM tb_user_notifications
LEFT JOIN tb_user_notifications_recap ON
tb_user_notifications.NumId = tb_user_notifications_recap.Notification
WHERE tb_user_notifications_recap.NIC_Handle='".$_SESSION_NICHANDLE."'

Avec JOINT LEFT j'ai toujours :
15-03-2018 - Sujet 2 - Lu
08-03-2017 - Sujet 1 - Lu

au lieu d'avoir :
22-09-2018 - Sujet 6 - Non lu
22-09-2018 - sujet 5 - Non lu
22-09-2018 - Sujet 4 - Non lu
17-03-2018 - Sujet 3 - Non lu
15-03-2018 - Sujet 2 - Lu
08-03-2017 - Sujet 1 - Lu
0
jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 4 649
23 sept. 2018 à 16:33


SELECT *
FROM tb_user_notifications U
LEFT JOIN tb_user_notifications_recap R ON U.id = R.notification
WHERE R.user = 'JA90717' OR R.user IS NULL
0
jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 4 649
23 sept. 2018 à 16:34
Ou encore
SELECT *
FROM tb_user_notifications U
LEFT JOIN tb_user_notifications_recap R ON U.id = R.notification AND R.user = 'JA90717'
0
jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 4 649
23 sept. 2018 à 16:37
Sachant que j'ai utillisé la structure de bdd suivante

-- Export de la structure de table test3. tb_user_notifications
CREATE TABLE IF NOT EXISTS `tb_user_notifications` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `sujet` varchar(50) DEFAULT NULL,
  `message` text,
  PRIMARY KEY (`id`),
  KEY `date` (`date`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

-- Export de données de la table test3.tb_user_notifications: ~0 rows (environ)
DELETE FROM `tb_user_notifications`;

INSERT INTO `tb_user_notifications` (`id`, `date`, `sujet`, `message`) VALUES
	(1, '2018-03-08 19:07:00', 'test1', 'dsfsdf'),
	(2, '2018-03-15 19:07:00', 'test2', 'dsfsdf'),
	(3, '2018-03-17 16:59:00', 'test3', 'dsfsdf'),
	(4, '2018-09-23 13:00:20', 'test4', 'dsfsdf'),
	(5, '2018-09-23 14:27:20', 'test5', 'dsfsdf'),
	(6, '2018-09-23 15:27:20', 'test6', 'dsfsdf');



-- Export de la structure de table test3. tb_user_notifications_recap
CREATE TABLE IF NOT EXISTS `tb_user_notifications_recap` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user` varchar(50) DEFAULT NULL,
  `notification` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user` (`user`),
  KEY `notification` (`notification`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- Export de données de la table test3.tb_user_notifications_recap: ~0 rows (environ)
DELETE FROM `tb_user_notifications_recap`;

INSERT INTO `tb_user_notifications_recap` (`id`, `user`, `notification`) VALUES
	(1, 'JA90717', 1),
	(2, 'JA90717', 2),
	(3, 'JS38579', 1);


NB: Lorsque tu nommes tes tables ou les champs qui s'y trouvent.. je te conseille de n'utiliser que des minuscules....
Et pour les champs "id" ..'id' suffit... pas besoin de complexifier en mettant des trucs du genre NumId
0
Sinistrus Messages postés 1017 Date d'inscription mercredi 12 décembre 2007 Statut Membre Dernière intervention 6 juin 2023 17
23 sept. 2018 à 17:33
C'est lui qui me manquais
OR R.user IS NULL
:'(
Merci pour cette nette clarification Jordan !

Je vais faire quelques tests en interne pour mieux comprendre les jointures.
Encore merci !
0