简体   繁体   English

MySQL SELECT COUNT(*)基于多个条件的两个表

[英]MySQL SELECT COUNT(*) From two tables based on multiple conditions

I have some sort of logic/sql challenge. 我有某种逻辑/ SQL挑战。 I have two tables that hold user data and are related by a id. 我有两个保存用户数据的表,它们由一个ID关联。 I would like to select the count of rows based on conditions from the two tables. 我想根据两个表中的条件选择行数。

Here are my tables. 这是我的桌子。

table one 表一

t_contacts
id  | name    | tel     | profession | email   | sex  | city    | state   |
int | varchar | varchar | int        | varchar | char | varchar | varchar |

table two 表二

t_contacts_meta
cid                | interest     |
int (t_contact_id) | varchar      |

from the table, contacts are stored on the t_contacts table and their interest stored on the t_contacts_meta table. 从表中,联系人存储在t_contacts表中,而兴趣存储在t_contacts_meta表中。 One contact could have multiple interests. 一位联系人可能有多个兴趣。

My issue is the SQL query. 我的问题是SQL查询。 example

 SELECT COUNT(*) as count 
 FROM t_contacts 
 where `state`='$state' 
    AND `profession`='$profession' 
    AND `sex`='$sex' 
    AND (
        `t_contacts_meta.interest`='$interest_1' 
        OR `t_contacts_meta.interest`='$interest_2 
        OR `t_contacts_meta.interest`='$interest_n'
    )

The sql i provided above is not my working code its just an example. 我上面提供的sql并不是我的工作代码,它只是一个示例。 I dont even know how to go about the query. 我什至不知道如何查询。

Thanks for the help :) 谢谢您的帮助 :)

You need to join the tables first: 您需要首先加入表:

SELECT COUNT(c.id) as count 
FROM t_contacts c
inner join t_contacts_meta m on m.cid = c.id
where 
(c.state=$state) AND 
(c.profession=$profession) AND 
(c.sex=$sex) AND 
(m.interest=$interest_1 OR m.interest=$interest_2 OR m.interest=$interest_n)

This is you're probably looking for: 这可能是您在寻找:

SELECT COUNT(C.id) AS [count]
FROM t_contacts C
INNER JOIN t_contacts_meta M ON M.cid = C.id
                               AND M.interest IN ('interest_1', 'interest_2', 'interest_n')
WHERE C.state = 'state_value'
    AND C.profession = 'profession_value'
    AND C.sex = 'sex_value'

Hope this will help you. 希望这会帮助你。

这不是逻辑挑战,您需要学习如何联接表

SELECT count(*) as count from t_contacts as T LEFT JOIN t_contacts_meta as M ON t.id=M.id WHERE T.state='$state' ...

You would use a join. 您将使用联接。 Your query will work if you use the following join in your query: 如果在查询中使用以下联接,则查询将起作用:

SELECT COUNT(t_contacts.id) as count FROM t_contacts LEFT JOIN t_contacts_meta ON t_contacts_meta.cid = t_contacts.id WHERE `state`='$state' AND `profession`='$profession' AND `sex`='$sex' AND (`t_contacts_meta.interest`='$interest_1' OR `t_contacts_meta.interest`='$interest_2 OR `t_contacts_meta.interest`='$interest_n')

I did not test this query, but you will at least get the idea, good luck! 我没有测试此查询,但您至少会明白,祝您好运!

To get a count of just rows from t_contacts, and not "count" any row from t_contacts more than once... 要获得来自t_contacts的仅行计数,而不是多次“计数”来自t_contacts的任何行...

One approach is to use an EXISTS predicate with a correlated subquery, eg 一种方法是将EXISTS谓词与相关子查询一起使用,例如

SELECT COUNT(*)
  FROM t_contacts t
 WHERE t.state      = 'fee'
   AND t.profession = 'fi'
   AND t.sex        = 'fo'
   AND EXISTS ( SELECT 1
                  FROM t_contacts_meta m 
                 WHERE m.cid = t.id
                   AND m.interest IN ('interest_1', 'interest_2', 'interest_n')
              )

An alternate approach is to use a JOIN operation, and count the "distinct" rows from t_contacts . 另一种方法是使用JOIN操作,并从t_contacts计算“不同”的行。 Assuming that the id column is the PRIMARY KEY of the table (or is at least guaranteed to be unique and not null.) 假设id列是表的PRIMARY KEY(或至少保证是唯一的而不是null)。

Note the use of the DISTINCT keyword inside the COUNT aggregate) 请注意在COUNT聚合内使用DISTINCT关键字)

SELECT COUNT(DISTINCT t.id)
  FROM t_contacts t
  JOIN t_contacts_meta m 
       ON m.cid = t.id
       AND m.interest IN ('interest_1', 'interest_2', 'interest_n')
 WHERE t.state      = 'fee'
   AND t.profession = 'fi'
   AND t.sex        = 'fo'

The original query appears to be including values of PHP variables in the SQL text, rather than using a prepared statement, eg 原始查询似乎在SQL文本中包含PHP变量的值,而不是使用准备好的语句,例如

   `state`='$state' 

Note that this pattern is vulnerable to SQL Injection. 请注意,此模式容易受到SQL注入的攻击。 Consider the actual SQL text if the value of $state contains a single quote, or contains something carefully constructed like: 如果$state的值包含单引号或包含精心构造的内容,请考虑实际的SQL文本:

    a' OR 'b'='b

Or something more nefarious, ala Little Bobby Tables 或更邪恶的东西,比如小鲍比桌子

The normal pattern is to use the mysqli_real_escape_string function, to "escape" values included in SQL text. 正常模式是使用mysqli_real_escape_string函数来“转义” SQL文本中包含的值。

http://php.net/manual/en/mysqli.real-escape-string.php http://php.net/manual/en/mysqli.real-escape-string.php

Try this 尝试这个

 SELECT COUNT(t_contacts_meta.interest) as count 
 FROM t_contacts_meta
 LEFT JOIN t_contacts
 ON t_contacts_meta.cid = t_contacts.id
 WHERE `t_contacts.state`='$state' 
    AND `t_contacts.profession`='$profession' 
    AND `t_contacts.sex`='$sex' 
    AND (
        `t_contacts_meta.interest`='$interest_1' 
        OR `t_contacts_meta.interest`='$interest_2 
        OR `t_contacts_meta.interest`='$interest_n'
    )

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM