简体   繁体   English

子查询-MySQL-PHP

[英]Subquery - MySQL - PHP

I have 2 tables. 我有2张桌子。

table2 structure: table2的结构:

  name
  os
  count

table1 structure: table1的结构:

 name

Examples of table2: Fred Android 50 Tom iOS 3 Tom iOS 3 Fred Android 1 Fred Android 1 James iOS 20 table2的示例:Fred Android 50 Tom iOS 3 Tom iOS 3 Fred Android 1 Fred Android 1 James iOS 20

Table1 has a list of names (unique). 表1列出了一个名称(唯一)。

My current query (stored in PHP variable $sqlx) is 我当前的查询(存储在PHP变量$ sqlx中)是

$sqlx = "SELECT COUNT(*) AS numberOfRows FROM table2 where name = 'Fred' AND  count < '6' AND os = 'iOS' GROUP BY name";

How do I make a subquery so that I don't have to enter 'Fred' so that the name is selected from table1? 如何进行子查询,而不必输入'Fred'以便从表1中选择名称?

Adding the IN means you will check for all items in this list. 添加IN意味着您将检查此列表中的所有项目。 Then using the second select query you are just pulling all names from table1. 然后使用第二个选择查询,您只需从表1中提取所有名称。 Here you can impose more conditions if you need to. 如果需要,您可以在此处施加更多条件。

$sqlx = "SELECT COUNT(*) AS numberOfRows FROM table2 WHERE name IN (SELECT name FROM table1) AND count < '6' AND os = 'iOS' GROUP BY name";

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

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