0

I have two tables I'm trying to query:

  • tbl_sermons
  • tbl_sermons-series

I've tried the MySQL query several different ways but it keeps on coming back with something similar to:

"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''tbl_sermons-series' ON tbl_sermons.series_sermon = 'tbl_sermons-series.id_seri' at line 3"

Here is the query:

SELECT tbl_sermons.title_sermon, tbl_sermons.slug_sermon, tbl_sermons.date_sermon, 'tbl_sermons-series.name_series' 
FROM tbl_sermons 
LEFT JOIN 'tbl_sermons-series' 
ON tbl_sermons.series_sermon = 'tbl_sermons-series.id_series';

I'm sure I'm doing something stupid...Can someone be my second set of eyes?

===

So I had used backticks at one point but that was throwing errors before. I went back to using backticks per the suggestions given (thanks) and now I receive the following error:

1054 - Unknown column 'tbl_sermons-series.name_series' in 'field list'

I am looking at the tbl_sermons-series table and it exists as does the field I am requesting (name_series). If I remove the request for name_series and just leave in tbl_sermons-series.id_series at the end of the LEFT JOIN (to the right of equals in the ON) it throws the same error about tbl_sermons.id_series!

Here are the tablet definitions for tbl_sermons (first) and tbl_sermons-series (second):

tbl_sermons tbl_sermons-series

The exact code that is currently failing is:

SELECT tbl_sermons.title_sermon, tbl_sermons.slug_sermon, tbl_sermons.date_sermon, `tbl_sermons-series`.`name_series` 
FROM tbl_sermons 
LEFT JOIN `tbl_sermons-series` 
ON tbl_sermons.series_sermon = `tbl_sermons-series`.`id_series`;
Dave Mackey
  • 4,306
  • 21
  • 78
  • 136
  • 1
    Get rid of the single quotes around `'tbl_sermons-series.id_series'` – mseifert Sep 05 '15 at 17:13
  • 1
    Yes you are right , you are doing something stupid and that is using single quotes. mysql treats it as string instead of some variable or keyword. – mannuscript Sep 05 '15 at 17:21
  • Thanks, I had originally used backticks but when that didn't work tried single quotes, now I've gone back to backticks and and am getting another error... – Dave Mackey Sep 05 '15 at 17:22
  • Let's see the actual definitions of the tables, then. I don't know what client you're using, but what we'll need is the output from the commands `describe tbl_sermons` and `describe tbl_sermons-series`. – Matt Gibson Sep 05 '15 at 17:30
  • 1
    Although--are you using backticks around the *whole* name, i.e. `\`tbl_sermons-series.id_series\``? Because what you probably want is `\`tbl_sermons-series\`.\`id_series\`` – Matt Gibson Sep 05 '15 at 17:32
  • 1
    (You could, by the way, avoid all this mucking around if you simply name your tables and columns without the hyphens. you wouldn't need to use any backticks at all if the name was `tbl_sermons_series`...) – Matt Gibson Sep 05 '15 at 17:38
  • If you have a different question, ask a different question, don't edit this question to invalidate existing answers. – durron597 Sep 05 '15 at 17:40
  • Matt - I've added the tablet definitions in a screenshot above. – Dave Mackey Sep 05 '15 at 17:42
  • PS I didn't create the tables, I'm just living with them. :) – Dave Mackey Sep 05 '15 at 17:43
  • 1
    You need to update your question with the *exact* code that's failing now, please. Or, as @durron597 says, ask a new question. Though I believe we'll probably come to the conclusion that your problem *is* actually a duplicate of the one this question is now marked a duplicate of, i.e. your problem is just a little misunderstanding of where and how to use backticks. – Matt Gibson Sep 05 '15 at 17:47
  • 1
    Given the schema you've posted and the query you've posted, I can't possibly see how you're getting that last error. [Here's a sqlfiddle showing your exact query working with a minimal version of the tables you've posted](http://sqlfiddle.com/#!9/cf18b0/1). Go make a cup of tea, come back, and make absolutely sure you're running the SQL you think you're running, and that you're getting the error you think you're getting. – Matt Gibson Sep 05 '15 at 18:01
  • Thanks. I'm not sure what is wrong, I'm just trying to work with the data to get it out of the DB, I'm going to export it to a CSV and manipulate that way...was trying to make my life easier, but that didn't happen. :) – Dave Mackey Sep 05 '15 at 18:08

1 Answers1

3

Don't use quotes around table or column names. If you have to escape the name use backticks

SELECT s.title_sermon, s.slug_sermon, s.date_sermon, ss.name_series
FROM tbl_sermons s
LEFT JOIN `tbl_sermons-series` ss ON s.series_sermon = ss.id_series
juergen d
  • 201,996
  • 37
  • 293
  • 362