La ligne suivante affiche un message d'erreur.Question de jointure mySQL
$query = 'SELECT * FROM products AS p LEFT JOIN categories AS c USING ON c.id = p.category_id WHERE c.name = "Galleri1"
AND p.status = "active"' ;
$Q = $this->db->query($query);
Structure de base de données. CATÉGORIES
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`shortdesc` varchar(255) NOT NULL,
`longdesc` text NOT NULL,
`status` enum('active','inactive') NOT NULL,
`parentid` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
...
...
PRODUIT
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`shortdesc` varchar(255) NOT NULL,
`longdesc` text NOT NULL,
`thumbnail` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`class` varchar(255) DEFAULT NULL,
`grouping` varchar(16) DEFAULT NULL,
`status` enum('active','inactive') NOT NULL,
`category_id` int(11) NOT NULL,
`featured` enum('true','false') NOT NULL,
`price` float(4,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
Message d'erreur
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON c.id = p.category_id WHERE c.name = "Galleri1" AND p.status = "active"' at line 1
SELECT * FROM products AS p LEFT JOIN categories AS c USING ON c.id = p.category_id WHERE c.name = "Galleri1" AND p.status = "active"
Q1. Quelqu'un pourrait-il souligner mon erreur plese?
Q2. Quelqu'un pourrait-il me dire comment l'écrire dans la classe Active Record de Codeigniter?
Merci d'avance.
Merci. Cela résout ce problème. Mais j'en ai un autre. Après cette requête, if ($ Q-> num_rows()> 0) { foreach ($ Q-> result_array() comme $ row) { $ data = array ( \t "id" => $ row [' id '], \t \t "name" => $ row [' "catégorie" nom '], \t "shortdesc" => $ row [' shortdesc '], \t \t ... \t \t => $ row ['categories.name'] Je reçois une erreur pour le dernier Comment attribuer le nom des catégories? – shin
'@ shin': il vaut mieux l'afficher comme une autre question – Quassnoi
Ok. J'ai posté ici http://stackoverflow.com/questions/1828406/ comment-obtenir-des-données-d'-une-table-jointe – shin