使用双引号加入SQL查询

时间:2016-04-04 11:46:14

标签: php mysql sql

我在PHP中有这个查询。

Select count(*) as total from ( select a.name,b.email from table1 a inner join table2 b on a.id = b.emailId ) myTable

我想用引号将其写为

$sql = "select count(*) as total from ( select a.name,b.email from table1 a inner join table2 b on a.id = b.emailId ) myTable"

但是我收到了一个错误。 如何在用PHP编写时处理单引号和双引号?

3 个答案:

答案 0 :(得分:1)

您编写的查询在MySQL中语法不正确。它需要子查询的别名:

Select count(*) as total
from (select a.name, b.email
      from table1 a inner join
           table2 b
           on a.id = b.emailId
     ) ab
-------^

但是,子查询是不必要的,所以我建议:

      select count(*)
      from table1 a inner join
           table2 b
           on a.id = b.emailId

答案 1 :(得分:0)

如果您不知道如何使用引号,请阅读strings in php

答案 2 :(得分:-1)

您的查询目前不正确。但是,要在PHP中构建查询,您可以使用双引号,然后使用单引号来使用如下所示的字符串:

编辑:

$my_name = 'HarshMakadia'; 
$sql = "SELECT id, name, description FROM your_table WHERE name ='".$my_name."'";