如何为这个$ sql做一个select count查询?

时间:2011-09-16 00:10:50

标签: php count

我需要这个查询:

$sql = "
SELECT STRAIGHT_JOIN DISTINCT 
       table1.pid, table1.fn, table2.did_id, table2.whodoc, table3.nname, table3.nid
FROM table1
JOIN table4 ON table1.pid = table4.pid 
JOIN table2 ON table4.did_id = table2.did_id
JOIN table3 ON table1.pid = table3.pid
WHERE kind = '$kind' 
  AND (
        (table3.nname LIKE '%$q%'
         OR table3.pat_notes LIKE '%$q%'
         OR table3.nid like '%$q%')
      )
ORDER BY table3.id DESC
LIMIT $startrow, 10
";

以这种格式重写:

$result = mysql_query("
SELECT * 
FROM some table 
LEFT JOIN other_table ON something.some_id = somesometable.somesomeid_id 
WHERE atable.a1field = '$something'
   OR the_atable.a2table_by = '$ok' 
  AND privacy = '$sorter'
  AND the_visit.why_visit LIKE '%$q%' 
   OR the_visit.height_onv LIKE '%$q%'
   OR the_visit.blood_pressure_onv LIKE '%$q%'
");

$num_rows = mysql_num_rows($result);

1 个答案:

答案 0 :(得分:1)

无法访问实际数据,我相信你可以这样做:

$sql = "
SELECT STRAIGHT_JOIN DISTINCT COUNT(*) AS Count
FROM table1
JOIN table3 ON table1.pid = table3.pid
WHERE kind = '$kind' 
  AND (
          table3.nname LIKE '%$q%'
       OR table3.pat_notes LIKE '%$q%'
       OR table3.nid like '%$q%'
)
";

不确定STRAIGHT_JOIN部分,因为我没有这样做。但其余的我认为应该有用。