我正在创建一个简单的数据库。我有一个如下所示的分类表:
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_name` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
产品表如下所示:
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`recipe_name` varchar(32) NOT NULL,
`category_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
现在我想获取如下数据:
{
"data": {
"menu": [
{
"id": "4821076d99ca1ad5fdb3aba31c19a8a7730533a6f7ea5b0a",
"category_name": "Italian",
"recipes": [
{
"id": "7728135",
"recipe_name": "Pizza",
},
{
"id": "7728136",
"recipe_name": "Garlik bread",
},
{
"id": "7951066",
"recipe_name": "Pasta",
}
]
},
]
},
"status": "1",
"message": ""
}
我使用下面的查询,但它不工作。。。
$query = "SELECT c.category_name as category_name, p.id,
p.recipe_name,p.category_id FROM
" . $this->table_name . " p
LEFT JOIN categories c
ON p.category_id = c.id";
使用上述查询,我得到的答复如下:
{
"data":[
{
"category_id":"5",
"category_name":"Healthy Eating",
"recipes":[
{
"id":"18",
"recipe_name":"GARDEN SALAD"
}
]
},
{
"category_id":"5",
"category_name":"Healthy Eating",
"recipes":[
{
"id":"19",
"recipe_name":"Vegetable salad"
}
]
}
]
}
1条答案
按热度按时间wgxvkvu91#
下面是一些示例代码。您需要调整mysql connect的详细信息。