1

我有一个搜索功能,可以根据输入来搜索 mysql 结果。现在我想在where子句中包含来自不同表的结果的点赞数()。

比如这个场景。I want to search "Dog" where count(like_id) >= 1.

我想要包含的 Likes 表是:TABLE_GLOBAL_LIKES.

的结构TABLE_GLOBAL_LIKES是: 字段:like_id, user_id, products_id, blog_id.

BOTHTABLE_GLOBAL_PRODUCTSTABLE_GLOBAL_LIKES有一个共同的领域products_idblog_id关联。

这是我希望包含 Likes 表的工作查询。

SELECT SQL_CALC_FOUND_ROWS p.*,
       CASE
           WHEN p.specials_new_products_price > 0.0000
                AND (p.expires_date > Now()
                     OR p.expires_date IS NULL
                     OR p.expires_date ='0000-00-00 00:00:00')
                AND p.status != 0 THEN p.specials_new_products_price
           ELSE p.products_price
       END price
FROM ".TABLE_GLOBAL_PRODUCTS." p
INNER JOIN ".TABLE_STORES." s ON s.blog_id = p.blog_id
WHERE MATCH (p.products_name,
             p.products_description) AGAINST ('*".$search_key."*')
  AND p.display_product = '1'
  AND p.products_status = '1' HAVING price <= ".$priceto_key."
  AND price >= ".$pricefrom_key."
ORDER BY p.products_date_added DESC, p.products_name

我是 mysql 查询的新手。请帮忙。

4

1 回答 1

1

尝试这个:

SELECT SQL_CALC_FOUND_ROWS p.*, COUNT(l.like_id)
       CASE
           WHEN p.specials_new_products_price > 0.0000
                AND (p.expires_date > Now()
                     OR p.expires_date IS NULL
                     OR p.expires_date ='0000-00-00 00:00:00')
                AND p.status != 0 THEN p.specials_new_products_price
           ELSE p.products_price
       END price
FROM ".TABLE_GLOBAL_PRODUCTS." p
INNER JOIN ".TABLE_STORES." s ON s.blog_id = p.blog_id
INNER JOIN ".TABLE_GLOBAL_LIKES." l ON l.blog_id = p.blog_id AND l.products_id = p.products_id
WHERE MATCH (p.products_name,
             p.products_description) AGAINST ('*".$search_key."*')
  AND p.display_product = '1'
  AND p.products_status = '1' HAVING price <= ".$priceto_key."
  AND price >= ".$pricefrom_key."
GROUP BY p.products_id
HAVING COUNT(l.like_id)>0
ORDER BY p.products_date_added DESC, p.products_name
于 2012-12-31T03:30:57.953 回答