如何对此 $sql 进行选择计数查询?

发布于 2024-12-05 09:12:27 字数 932 浏览 2 评论 0原文

我需要这个查询:

$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);

I need this query:

$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
";

To be rewritten in this format:

$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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳公公是暖光 2024-12-12 09:12:27

由于无法访问实际数据,我相信您可以执行以下操作:

$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 部分,因为我还没有这样做。但其余的我认为应该有效。

Not having access to the actual data, I believe you could do something like this:

$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%'
)
";

Not real sure about the STRAIGHT_JOIN part, since I've not done that. But the rest I think should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文