基于类别的Mysql查询

时间:2013-04-07 03:30:33

标签: mysql database

您好我有3张桌子如下:

业务:

business_id business_name cat_id sub_cat_id
  1           bz1           1      1001
  2           bz2           1      1005
  3           bz3           2      2001
  4           bz4           1      1001

business_category:

cat_id cat_name
1        Restaurant
2        Food
3        Travel

business_sub_category:

b_sub_cat_id b_subcat_name b_maincat_id
1001           Italian        1
1002           French         1
1003           Asian          1
2001           Bagels         2

现在,当我想使用类似的基于b_subcat_name查找业务时,我的查询似乎效果不佳。这是我的疑问:

SELECT `b`.`business_name`, `b`.`cat_id`, `b`.`business_id`, `bc`.`cat_name`, `bsc`.`b_subcat_name`, `bsc`.`b_sub_cat_id` FROM `business` AS `b` INNER JOIN `business_category` AS `bc` ON b.cat_id = bc.cat_id INNER JOIN `business_sub_category` AS `bsc` ON b.sub_cat_id = bsc.b_sub_cat_id WHERE (bc.cat_name like 'italian%') OR (bc.cat_name like '%italian') OR (bc.cat_name = 'italian')

谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您应该在b_subcat_name子句中使用WHERE,因为没有cat_name的值包含Italy

尝试改变这一点:

SELECT `b`.`business_name`, `b`.`cat_id`, `b`.`business_id`, `bc`.`cat_name`, `bsc`.`b_subcat_name`, `bsc`.`b_sub_cat_id` FROM `business` AS `b` INNER JOIN `business_category` AS `bc` ON b.cat_id = bc.cat_id INNER JOIN `business_sub_category` AS `bsc` ON b.sub_cat_id = bsc.b_sub_cat_id WHERE (bc.cat_name like 'italian%') OR (bc.cat_name like '%italian') OR (bc.cat_name = 'italian')

SELECT `b`.`business_name`, `b`.`cat_id`, `b`.`business_id`, `bc`.`cat_name`, `bsc`.`b_subcat_name`, `bsc`.`b_sub_cat_id` FROM `business` AS `b` INNER JOIN `business_category` AS `bc` ON b.cat_id = bc.cat_id INNER JOIN `business_sub_category` AS `bsc` ON b.sub_cat_id = bsc.b_sub_cat_id WHERE (bsc.b_subcat_name like 'italian%') OR (bsc.b_subcat_name like '%italian') OR (bsc.b_subcat_name = 'italian')