简体   繁体   English

如何计算表中具有其他表中特定属性的行

[英]how to count rows in table that has specific property in another table

table1
--------------
| sn | class |
--------------

table2
----------------
| id | student |
---------------- 

all are int as sn is table1 is linked to student in table2 sn , id are auto increasing. 因为sn是table1,所以所有都是int ,而table2中的sn链接到了学生snid会自动增加。 when inserting data to table2 student column is same as sn in table 1 将数据插入到table2学生列中时与表1中的sn相同

now I want to select student in table2 but only those whose class in table1 is "3" my syntax is thus; 现在我想在表2中选择student ,但是只有那些在表1中的class是“ 3”的学生,我的语法才是;

$count = mysql_query(SELECT student from table2 whose class in table1 =3)

so that i can count them by saying 这样我就可以说

$quantity = mysql_num_rows($count)

now my problem is if sql also have this whose keyword, or how do i go about this. 现在我的问题是,如果sql也具有此关键字,或者我该如何处理。

$count = mysql_query(SELECT student from table2 whose class in table1 =3)

You need to join the tables in order to filter the results properly. 您需要连接表才能正确过滤结果。

(1) This will give you the number of students for class 3. (1)这将为您提供3年级的学生人数。

$count = mysql_query(
  'SELECT COUNT(t2.student) 
   FROM table2 t2 
   INNER JOIN table1 t1
     ON t1.sn = t2.student
     AND t1.class = 3'
);

(2) This will give you all classes and the number of students for each. (2)这将为您提供所有课程以及每个课程的学生人数。

$count = mysql_query(
  'SELECT t1.class, COUNT(t2.student) 
   FROM table2 t2 
   INNER JOIN table1 t1
     ON t1.sn = t2.student
   GROUP BY t1.class
   ORDER BY t1.class'
);

(3) This will give you all classes and the students list. (3)这将为您提供所有班级和学生名单。

$list = mysql_query(
  'SELECT t1.class, GROUP_CONCAT(t2.student SEPARATOR ',') 
   FROM table2 t2 
   INNER JOIN table1 t1
     ON t1.sn = t2.student
   GROUP BY t1.class
   ORDER BY t1.class'
);

You should join those two tables and limit your result to those which have table1.class = 3 您应该将这两个表连接起来,并将结果限制为具有table1.class = 3的那些表

SELECT
    student
FROM
    table2 a
    JOIN table1 b ON (a.student = b.sn)
WHERE b.class = 3

If you want a count you could also do it through SQL by using aggregate function 如果要计数,也可以使用聚合函数通过SQL进行计数

SELECT
    COUNT(student)
FROM
    table2 a
    JOIN table1 b ON (a.student = b.sn)
WHERE b.class = 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Select 在另一个表中有多个属性的特定行 - Select specific rows which has several attributes in another table 如何计算从特定行开始的表的所有行 - how to count all the rows of a table starting from specific row 如何计算具有特定ID的联接表中的所有行 - How to count all rows in a joined table with a specific ID 如何根据表字段中的数据对特定行进行分页? - how to count specific rows based on data in table fields for pagination? 如何使用特定条件计算 SQL 表中的行数? - how to count the number of rows in SQL table using specific criteria? 如何计算一列与另一张表的一列匹配的行数 - How to COUNT the number of rows where a column matches a column of another table Laravel select 如果 id 存在于另一个表中并且列具有该表上的特定值,则所有行 - Laravel select all rows if id exists in another table and a column has specific values on that table MYSQL:计算另一个表中与第一个表中的行相对应的行数 - MYSQL: Count # of rows in another table corresponding to rows in first table MySQL:如何返回表中的所有行,并从另一个表中计算具有匹配ID的行数 - MySQL: How to return all rows in a table and count the amount of rows with matching ID from another table 如何计算数据库表中的行? - How to count rows in database table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM