如何在sql中读取当前值

时间:2012-04-03 07:38:54

标签: php mysql search select count

我创建了一个搜索功能:

SELECT
    tbl_place.id AS id, 
    tbl_place.metalink AS metalink, 
    tbl_place.title AS title, 
    tbl_place.img_thumbnail AS img_thumbnail, 
    tbl_place.category1_id AS category1_id, 
    tbl_place.category2_id AS category2_id, 
    tbl_place.category3_id AS category3_id, 
    tbl_place.region_id AS region_id, 
    tbl_place.subregion_id AS subregion_id, 
    tbl_place.summary AS summary, 
    (SELECT COUNT(tbl_place.title) FROM tbl_place) AS total_show_up
FROM tbl_place 
WHERE 
    tbl_place.title LIKE '%$keywords[0]%' AND
    tbl_place.title LIKE '%$keywords[1]%'

如果我使用关键字搜索:“Pizza Store”,它将显示如下内容:

* Pizza Store
* Pizza Store Manhattan
* Pizza Store California
* Pizza Store California
* Pizza Store Texas
* Pizza Store California

我想要做的是,还要计算每个tbl_place.title在查询仍然使用关键字进行搜索时显示的次数。 我希望结果变成这样:

* Pizza Store - showed 1 time
* Pizza Store Manhattan - showed 1 time
* Pizza Store California - showed 3 times
* Pizza Store California - showed 3 times
* Pizza Store Texas - showed 1 time
* Pizza Store California - showed 3 times

是否有任何语法在SELECT-ing时读取tbl_place的当前值?

我相信我在查询的这一部分有所改变:

(SELECT COUNT(tbl_place.title) FROM tbl_place) AS total_show_up

1 个答案:

答案 0 :(得分:2)

尝试这样的方法:

SELECT
   p1.title, pj.total
FROM 
   pizza p1
JOIN 
   (
    SELECT 
       p2.title title, COUNT(p2.id) total
    FROM pizza p2 
    WHERE p2.title LIKE '%Pizza%' AND p2.title LIKE '%Store%'
    GROUP BY p2.title
   ) AS pj
ON p1.title=pj.title