Mysql résultat en plusieurs exemplaires
Résolu
houba_houbi
-
Utilisateur anonyme -
Utilisateur anonyme -
Bonjour,
J'ai 3 tables Mysql : llivres, auteurs et catégories.
J'ai un formulaire de type : |_____________| rechercher
Je souhaiterai que quelque soit le mot clé saisi dans ce formulaire, celui-ci soit recherché dans mes 3 tables pour certaines colonnes.
J'ai donc commencer à écrire ceci :
SELECT *
FROM shop_livres, shop_auteurs, shop_categories
WHERE shop_livres.titre LIKE %MotCle% OR shop_livres.resume LIKE %MotCle% OR shop_auteurs.nom LIKE %MotCle% OR shop_auteurs.prenom LIKE %MotCle% OR shop_categories.libelle_categories LIKE %MotCle%
ORDER BY shop_livres.parution DESC
Ce qui fonctionne... mais si le livre répond à tous les critères, il ressort plusieurs fois !
Comment faire pour qu'il ne ressorte qu'une seule fois ?
Merci pour votre aide
Je débute....
J'ai 3 tables Mysql : llivres, auteurs et catégories.
J'ai un formulaire de type : |_____________| rechercher
Je souhaiterai que quelque soit le mot clé saisi dans ce formulaire, celui-ci soit recherché dans mes 3 tables pour certaines colonnes.
J'ai donc commencer à écrire ceci :
SELECT *
FROM shop_livres, shop_auteurs, shop_categories
WHERE shop_livres.titre LIKE %MotCle% OR shop_livres.resume LIKE %MotCle% OR shop_auteurs.nom LIKE %MotCle% OR shop_auteurs.prenom LIKE %MotCle% OR shop_categories.libelle_categories LIKE %MotCle%
ORDER BY shop_livres.parution DESC
Ce qui fonctionne... mais si le livre répond à tous les critères, il ressort plusieurs fois !
Comment faire pour qu'il ne ressorte qu'une seule fois ?
Merci pour votre aide
Je débute....
A voir également:
- Mysql résultat en plusieurs exemplaires
- Resultat foot - Télécharger - Vie quotidienne
- Lexer resultat - Télécharger - Sport
- Mysql community server - Télécharger - Bases de données
- 5/90 resultat ✓ - Forum Excel
- Avoir les résultats du dernier tirage du loto :) - Forum Linux / Unix
5 réponses
Essaie : SELECT disctinct(*)
Sinon tu fais SELECT distinct(shop_livres.clé_primaire), shop_livres.*, shop_auteurs.*, shop_categories.*
Sinon tu fais SELECT distinct(shop_livres.clé_primaire), shop_livres.*, shop_auteurs.*, shop_categories.*
Ok, Voici le code (généré par Dreamweaver) :
<?php require_once('Connections/boutique.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$MotCle_resultat = "-1";
if (isset($_POST['MotCle'])) {
$MotCle_resultat = $_POST['MotCle'];
}
mysql_select_db($database_boutique, $boutique);
$query_resultat = sprintf("SELECT * FROM shop_livres, shop_auteurs, shop_categories WHERE shop_livres.titre LIKE %s OR shop_livres.resume LIKE %s OR shop_auteurs.nom LIKE %s OR shop_auteurs.prenom LIKE %s OR shop_categories.libelle_categories LIKE %s", GetSQLValueString("%" . $MotCle_resultat . "%", "text"),GetSQLValueString("%" . $MotCle_resultat . "%", "text"),GetSQLValueString("%" . $MotCle_resultat . "%", "text"),GetSQLValueString("%" . $MotCle_resultat . "%", "text"),GetSQLValueString("%" . $MotCle_resultat . "%", "text"));
$resultat = mysql_query($query_resultat, $boutique) or die(mysql_error());
$row_resultat = mysql_fetch_assoc($resultat);
$totalRows_resultat = mysql_num_rows($resultat);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document sans titre</title>
</head>
<body>
<p>Résultat de la recherche pour : xxxxMOTCLExxxx</p>
<?php if ($totalRows_resultat == 0) { // Show if recordset empty ?>
<p>Il n'y a pas de résultat pour votre requête.</p>
<?php } // Show if recordset empty ?>
<p><strong>Ouvrages correspondants :</strong></p>
<?php do { ?>
<table>
<tr>
<td rowspan="2"><a><img alt="" width="100" height="75" /></a></td>
<td><strong><?php echo $row_resultat['titre']; ?></strong></td>
<td><strong>Prix <?php echo $row_resultat['prix_ttc']; ?>€</strong></td>
</tr>
<tr>
<td><strong><a> </a></strong><?php echo $row_resultat['nom']; ?><?php echo $row_resultat['prenom']; ?>- Paru le <strong><?php echo $row_resultat['parution']; ?></strong></td>
<td><a>Plus de détails</a></td>
</tr>
</table>
<?php } while ($row_resultat = mysql_fetch_assoc($resultat)); ?>
<p> </p>
</body>
</html>
<?php
mysql_free_result($resultat);
?>
<?php require_once('Connections/boutique.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$MotCle_resultat = "-1";
if (isset($_POST['MotCle'])) {
$MotCle_resultat = $_POST['MotCle'];
}
mysql_select_db($database_boutique, $boutique);
$query_resultat = sprintf("SELECT * FROM shop_livres, shop_auteurs, shop_categories WHERE shop_livres.titre LIKE %s OR shop_livres.resume LIKE %s OR shop_auteurs.nom LIKE %s OR shop_auteurs.prenom LIKE %s OR shop_categories.libelle_categories LIKE %s", GetSQLValueString("%" . $MotCle_resultat . "%", "text"),GetSQLValueString("%" . $MotCle_resultat . "%", "text"),GetSQLValueString("%" . $MotCle_resultat . "%", "text"),GetSQLValueString("%" . $MotCle_resultat . "%", "text"),GetSQLValueString("%" . $MotCle_resultat . "%", "text"));
$resultat = mysql_query($query_resultat, $boutique) or die(mysql_error());
$row_resultat = mysql_fetch_assoc($resultat);
$totalRows_resultat = mysql_num_rows($resultat);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document sans titre</title>
</head>
<body>
<p>Résultat de la recherche pour : xxxxMOTCLExxxx</p>
<?php if ($totalRows_resultat == 0) { // Show if recordset empty ?>
<p>Il n'y a pas de résultat pour votre requête.</p>
<?php } // Show if recordset empty ?>
<p><strong>Ouvrages correspondants :</strong></p>
<?php do { ?>
<table>
<tr>
<td rowspan="2"><a><img alt="" width="100" height="75" /></a></td>
<td><strong><?php echo $row_resultat['titre']; ?></strong></td>
<td><strong>Prix <?php echo $row_resultat['prix_ttc']; ?>€</strong></td>
</tr>
<tr>
<td><strong><a> </a></strong><?php echo $row_resultat['nom']; ?><?php echo $row_resultat['prenom']; ?>- Paru le <strong><?php echo $row_resultat['parution']; ?></strong></td>
<td><a>Plus de détails</a></td>
</tr>
</table>
<?php } while ($row_resultat = mysql_fetch_assoc($resultat)); ?>
<p> </p>
</body>
</html>
<?php
mysql_free_result($resultat);
?>
Merci Passarinho44,
Ta requête ne fonctionne malheureusement pas avec *, je l'ai retourné dans tous les sens mais comme je ne la connais pas, je n'ai peut être pas indiquée la bonne syntaxe...
En revanche, tu m'as mis sur la voie !!!!!
En recherchant DISTINCT je suis tombée sur GROUP BY.
En rajoutant GROUP BY shop_livres.id_livres, je n'ai plus de doublons !!!
Voilà c'est résolu,
Merci à vous tous.
Ta requête ne fonctionne malheureusement pas avec *, je l'ai retourné dans tous les sens mais comme je ne la connais pas, je n'ai peut être pas indiquée la bonne syntaxe...
En revanche, tu m'as mis sur la voie !!!!!
En recherchant DISTINCT je suis tombée sur GROUP BY.
En rajoutant GROUP BY shop_livres.id_livres, je n'ai plus de doublons !!!
Voilà c'est résolu,
Merci à vous tous.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question