繁体   English   中英

使用PHP进行MySql查询的最佳实践

[英]Best practice in MySql query with PHP

我有三个表master_category,category_group文章如下
1) master_category

cat_id             cat_name
------------------------------
1                   Movies
2                   Hollywood
3                   Bollywood
4                   Hollywood Actress
5                   Hollywood Actors
6                   Mobiles
7                   Sony
8                   Apple
9                   LG
10                  Sony Accessories

1) category_group

   id             Main_Group         Sub_Group
--------------------------------------------------
   1                  1                  2
   2                  1                  3
   3                  2                  4
   4                  2                  5
   5                  6                  7
   6                  6                  8
   7                  6                  9
   8                  7                  10      
 --------------------------------------------------

2) 文章

  id                    title                    description
----------------------------------------------------------------
  1                   sample title                some description

表记录是这样的,当我单击电影时,我需要获取包含单词“电影”,“好莱坞”,“好莱坞”,“好莱坞女星”,“好莱坞演员”等的文章,即(子组和子组等)

我已经做到了

SELECT names.* FROM master_category mc LEFT JOIN category_group sc #level1
ON sc.main_group = mc.cat_id LEFT JOIN category_group sc2 #level2
ON sc2.main_group = sc.sub_group LEFT JOIN master_category names
ON sc.sub_group = names.cat_id OR sc2.sub_group = names.cat_id
where mc.cat_name = 'movies' order by names.cat_id;

从这个查询中我可以得到cat_name

$ result是我们从该查询结果中获得的数组

$i=0;
foreach($result as $row)
{
  if($i!=0) { $query_condition.= "or "; } 
  $query_condition.= " title like '%".$row['cat_name']."%' or description like '%".$row['cat_name']."%'";
}

得到这样的结果

  $query_condition = " title like '%Movies%' or description like '%Movies%' or title like '%Bollywood%' or description like '%Bollywood%' or title like '%Hollywood%' or description like '%Hollywood%' or title like '%Hollywood Actress%' or description like '%Hollywood Actress%' or title like '%Hollywood Actor%' or description like '%Hollywood Actor%'

然后我写了最终查询如下

     select * from articles where $query_condition

这是最佳做法吗? 如果我们有更多的类别时间来获得最终结果,将会增加。 还有其他最好的方法来获得快速结果吗? 请告诉我

这可能会帮助您查询数据库,但是您需要提高其性能

SELECT DISTINCT    
           a.title,
           a.description
FROM       articles a
           INNER JOIN
           (
              SELECT (@clicked_cat_id := cat_id) cat_id,
                     cat_name
              FROM   master_category
              WHERE  Upper(cat_name) = Upper(@clicked_cat_name := "Movies")
              UNION
              SELECT     b.cat_id,
                         b.cat_name
              FROM       master_category b
                         INNER JOIN category_group c
                           ON b.cat_id = c.sub_group
              WHERE      c.main_group = @clicked_cat_id ) c
WHERE      a.title LIKE concat('%',c.cat_name,'%')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM