在sql中执行以下查询有什么问题

时间:2014-12-02 13:23:42

标签: php sql

SELECT jobs.id,jobs.title, jobs.budget, jobs.views_count, jobs.is_active, count(job_applications.id) as count
    FROM jobs 
    LEFT JOIN job_applications ON job_applications.job_id = jobs.id WHERE jobs.user_id ='.$id.'' AND jobs.title=''.$title.' group by jobs.id

此查询提供错误:

语法错误,意外'=',期待','或';'

4 个答案:

答案 0 :(得分:3)

问题在于PHP,而不是SQL。

你搞砸了单引号。

更新了查询:

$sql = "SELECT jobs.id,jobs.title, jobs.budget, jobs.views_count, jobs.is_active, count(job_applications.id) as count
        FROM jobs 
        LEFT JOIN job_applications ON job_applications.job_id = jobs.id WHERE jobs.user_id ='$id' AND jobs.title='$title' group by jobs.id";

答案 1 :(得分:1)

试试这个

$your_query = "SELECT jobs.id,jobs.title, jobs.budget, jobs.views_count, jobs.is_active, count(job_applications.id) as count FROM jobs 
    LEFT JOIN job_applications ON job_applications.job_id = jobs.id WHERE jobs.user_id =".$id." AND jobs.title='".$title."' group by jobs.id";

答案 2 :(得分:1)

注意你的报价。这是您的查询修复而不使用双引号:

$query = 'SELECT 
    jobs.id,
    jobs.title, 
    jobs.budget, 
    jobs.views_count, 
    jobs.is_active, 
    count(job_applications.id) as count
    FROM jobs 
LEFT JOIN job_applications ON job_applications.job_id = jobs.id 
WHERE 
    jobs.user_id ='.$id.' 
    AND jobs.title='.$title.' 
group by jobs.id';

如果$title与字符串有关,请使用以下内容,如@daiscog所指出的那样:

$query = 'SELECT 
    jobs.id,
    jobs.title, 
    jobs.budget, 
    jobs.views_count, 
    jobs.is_active, 
    count(job_applications.id) as count
    FROM jobs 
LEFT JOIN job_applications ON job_applications.job_id = jobs.id 
WHERE 
    jobs.user_id ='.$id.' 
    AND jobs.title=\''.$title.'\' 
group by jobs.id';

答案 3 :(得分:1)

更改此

jobs.user_id ='.$id.'' AND jobs.title=''.$title.'

jobs.user_id ='.$id.' AND jobs.title='.$title.'