Page 1 of 1

Clarification: Sql(Solved)

Posted: Wed Aug 10, 2005 7:09 am
by raghavan20
This does not work

Code: Select all

select  `UserName`, `Message`
from `ChatMessages_tbl`, `ChatUsers_tbl` 
where `ChatMessages_tbl.UserId` = `ChatUsers_tbl.Id`
If I take off the "`" symbol on the fields in the where parameter, it works..why???

Code: Select all

select  `UserName`, `Message` from `ChatMessages_tbl`, `ChatUsers_tbl` where
ChatMessages_tbl.UserId = ChatUsers_tbl.Id

Posted: Wed Aug 10, 2005 7:27 am
by Skittlewidth
To quote burrito:
mysql allows you to use the backticks so that you can use reserved words, operators and keywords for table and field names.

ex:

a field name this-field would not be valid because of the minus (-) sign.

so if you tried this:

Code:

select * from myTable where this-field = 'joe'



the query would bomb out...whereas

Code:

select * from `myTable where `this-field` = 'joe'



would work.
For the rest of this post see viewtopic.php?t=36594

Posted: Wed Aug 10, 2005 7:46 am
by raghavan20
That was not my problem.
My problem is the query did not work when I had the backticks on; only the second format worked.

Code: Select all

`ChatMessages_tbl.UserId`
It says that this field cannot be found
but this works

Code: Select all

ChatMessages_tbl.UserId

Posted: Wed Aug 10, 2005 7:56 am
by skhale
Use

Code: Select all

`ChatMessages_tbl`.`UserId`
Make sure the table name has its own backticks and the field name has its own

Posted: Wed Aug 10, 2005 8:11 am
by raghavan20
Oh!!! I did not think of that. It works as you said.

Earlier it was assuming the whole thing(`ChatMessages_tbl.UserId`) to be a field and reported that the field could not be found

Now with backticks on table name, unmarked dot operator and the backticked field name, it works fine.


cheers!!!