开发者

MySQL join problem

开发者 https://www.devze.com 2022-12-14 00:53 出处:网络
I want to join a pages table and menu table. CREATE TABLE IF NOT EXISTS `pages` ( `id` int(11) NOT NULL AUTO_INCREMENT,

I want to join a pages table and menu table.

CREATE TABLE IF NOT EXISTS `pages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  `keywords` varchar(255) NOT NULL D开发者_JS百科EFAULT '',
  `description` varchar(255) NOT NULL DEFAULT '',
  `path` varchar(255) NOT NULL DEFAULT '',
  `content` text NOT NULL,
  `status` enum('active','inactive') NOT NULL DEFAULT 'active',
  `category_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;



CREATE TABLE IF NOT EXISTS `menus` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `shortdesc` varchar(255) NOT NULL,
  `page_id` varchar(60) NOT NULL,
  `status` enum('active','inactive') NOT NULL,
  `parentid` int(11) NOT NULL,
  `order` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=79 ;

I have errors with the following SQL.

function generateTree(&$tree, $parentid = 0) {
$res = $this->db->query('SELECT M.*, P.name AS PageName
    WHERE M.parentid = $parentid
    ORDER BY M.order asc, M.parentid asc
    FROM menus AS M
    LEFT JOIN pages AS P
    ON P.id = M.page_id');
...
...

Can you tell what I am doing wrong?

Thanks in advance.


You've got your SQL syntax mixed up

$res = $this->db->query('
SELECT 
 M.*, P.name AS PageName
FROM
 menus AS M 
 LEFT JOIN pages AS P ON P.id = M.page_id
WHERE 
 M.parentid = $parentid
ORDER BY 
 M.order asc, M.parentid asc
');

BTW, you should bot be using variables in the SQL string. Use parameterized queries instead (mysyqli*).

0

精彩评论

暂无评论...
验证码 换一张
取 消