0

I am trying to populate a table by executing the below query for a php application:

$sql3 = "SELECT distinct(`t1.testName`), `t2.comments AS C1` from `sample AS t1` left join `sample AS t2` ON `t1.testName`= `t2.testName` where `t1.buildNumber`= 181 and `t2.buildNumber`= 180 and `t1.errorStackTrace` is not null";

$result3 = mysqli_query($dbconnect,$sql3);

if(!mysqli_query($dbconnect, $sql3)){
    printf("error message: %s\n",mysqli_error($dbconnect));
}

I am seeing the following error returned:

error message: Table 'testdata.sample as t1' doesn't exist

I have tried a lot to fix this but couldn't. The query runs fine when run on mysql. Any help would be appreciated. Thanks

1
  • use proper back quotes or remove $sql3 = "SELECT distinct(t1.testName), t2.comments AS C1 from sample AS t1 left join sample AS t2 ON t1.testName= t2.testName where t1.buildNumber= 181 and t2.buildNumber= 180 and t1.errorStackTrace is not null"; Commented Jul 28, 2016 at 5:21

3 Answers 3

1

You have to use the backticks only arround table names or column names not including the alias:

$sql3 = "SELECT distinct(`t1`.`testName`), `t2`.`comments` AS C1 from `sample` AS ` left join `sample` AS t2 ON t1.testName= t2.testName where t1.buildNumber= 181 and t2.buildNumber= 180 and t1.errorStackTrace is not null";
Sign up to request clarification or add additional context in comments.

Comments

1

You are escaping the table name incorrectly. Use this raw query:

SELECT DISTINCT(t1.testName),
       t2.comments AS C1
FROM `sample` AS t1
LEFT JOIN `sample` AS t2
    ON t1.testName = t2.testName
WHERE t1.buildNumber = 181 AND
      t2.buildNumber = 180 AND
      t1.errorStackTrace IS NOT NULL

I don't think you really need backticks anywhere. But in any case, only column names need to be backticked, never aliases, e.g.

t1.`testName` but NOT `t1.testName`

Comments

0

Wrong back quote, try this:

$sql3 = "SELECT distinct(t1.`testName`), t2.`comments` AS C1 from `sample` AS t1 left join `sample` AS t2 ON t1.`testName`= t2.`testName` where t1.`buildNumber`= 181 and t2.`buildNumber`= 180 and t1.`errorStackTrace` is not null";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.