簡體   English   中英

如何計算WHERE子句中的字段

[英]How to count fields on WHERE Clause

我想問一些關於WHERE子句的計數。 我對此進行了研究,但未找到任何解決方案。

我有2張桌子:

表1包含:

id, level, instansi

表2包含:

id, level, table1ID

題:

僅當table2 less than 2的行數table2 less than 2如何顯示table1.instansi

條件

table2.table1ID = table1.id
select t1.* 
from table1 t1 join table2 t2 on t2.table1ID=t1.id
group by t1.id
having count(*)<2

一種方法是進行查詢以選擇所有行,然后對行進行計數

$ query =(“ SELECT * from table”);

$結果= mysqli_query($ con,$ query);

$ count = mysqli_num_rows($ result);

在條件中使用$ count

您需要使用Have子句在where子句中使用count

select t1.instanci
from table1 t1 inner join table2 t2
   on t1.table1id=t2.table2id
group by t2.table2id
having count(t2.table2id)<2

您需要使用Haveing子句為聚合函數(例如“ count”)添加條件

http://www.w3schools.com/sql/sql_having.asp

Select t1.instanci From table1 t1 inner join table2 t2 on t1.table1id=t2.table2id
Group by t2.table2id Having count(t2.table2id)<2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM