Jump to content

Select column names that contain spaces??


Round

Recommended Posts

I am trying to select column names from a table in our db that contain spaces, But I can't ???

$sql = "select  distinct grp no from tbl_studiag_num where stu no=\"$stu_id\" ";

Which causes an error message.

I have also tried:-

$sql2 = "select  distinct 'grp no' from tbl_studiag_num where 'stu no'=\"$stu_id\" ";

Which doesn't cause the error message, but doesn't return any rows?!? But I know there are matching rows?!?

I know its not good practice to give tables and columns, names containg spaces but I didn't create the DB and I can't rename them for obvious reasons.

Many Thanks
Link to comment
Share on other sites

it will treat 'stu no' as a string, which probably isn't equal to any of your student ids

use square brackets instead: [stu no]


On another note, I always wondered why it was 'bad practice' to use spaces? Other than a bit more typing for the SQL Coder... it's certainly more legible than CamelCode or less annoying than_using_underscores...
Link to comment
Share on other sites

Thanks MCP.

I agree with you, why is it considered as "bad practice". For me I don't know, I just make sure I use underscores as I was told it's best to. The thing is, Yes it caused a problem for me this time, but if I'd been taught the spaces method it wouldn't have been an issue.
However I'm not going to shoot the mesenger either because chances are the fairly useless underscores knowledge has probably been passed on and no one knows the actual reason for it. I agree if people want to say its so that there is a standard, but if no one used them the satndard would be no underscores and people using [..]. But one thing is for sure it is easier to type without the underscore because it's not even a single button press, it always requires shift.

Cheers
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.