Click to See Complete Forum and Search --> : SQL: Selecting from "column names" with spaces in?


Nibinaear
February 17th, 2005, 06:52 PM
I have a column name called "Percentage mark" in a table called assessments. Because the column name has a space in, any query like:

select Percentage mark from assessments;

doesn't work.

If I try,

select "Percentage mark" from assessments;

that doesn't work either, and the same story for single quotes. This problem doesn't seem well documented, but I did find one suggestion which says to use square brackets around the column name like this:

select [Percentage mark] from assessments;

but that doesn't work either!

I'm using MYSQL and trying to get the results using php. I've tried it in MYSQL Control Pannel, so the problem isn't php.

khp
February 17th, 2005, 09:02 PM
I have a column name called "Percentage mark" in a table called assessments. Because the column name has a space in.

Why would you ever do that ?. How was the table even created in the first place ?. Are you sure it's not just an underscore you are somehow failing to read ?
To see how mysql deals that whitespaces in table names I would suggest you do a dump of the database, using mysqldump. Assuming your database is very big you might want to check the commandline options for mysqldump, to get only the table definitions.

Nibinaear
February 18th, 2005, 09:06 AM
No definitely not. The tables were created by someone else, and as they are for a php/mysql assignment, I'm not allowed to change them.

Also, because I can't select the table, there is no way of altering it to change the table name, if I wanted to break the rules!

mysqldump doesn't seem to work.

khp
February 18th, 2005, 03:37 PM
mysqldump doesn't seem to work.

Ehh ?, if you want people to help you, you are going to have to be more informative.

Oh and what response do you get from a query like 'describe assessments;' ?

Doctor Luz
February 18th, 2005, 05:04 PM
You have to use the backtick `


select `Percentage mark` from assessments;


http://dev.mysql.com/doc/mysql/en/legal-names.html

Nibinaear
February 18th, 2005, 05:44 PM
Ah, genius, thanks a lot.

Actually I've decided to change the name in my own tables now anyway as the only one I have to keep the same isn't important.

That was pretty uncalled for KHP, you only seem to want to deny the problem anyway.

Doctor Luz
February 18th, 2005, 06:09 PM
That was pretty uncalled for KHP, you only seem to want to deny the problem anyway.

Is this for me? Sorry, I don know what you want to say with this

khp
February 19th, 2005, 12:46 PM
Is this for me? Sorry, I don know what you want to say with this

No I think it was for me, though quite ill concived.


That was pretty uncalled for KHP, you only seem to want to deny the problem anyway.

Ehhh ?, when did I ever deny the problem ?.
I suggested a surefire way, for you to solve the problem, and you respond with a mindnumming 'that doesn't seem to work'
Do you think it unresonable to expect you, to put even the least bit of effort into your own problem ?
I will ofcourse be quite happy, not to respond to any of your future messages.

Nibinaear
February 22nd, 2005, 04:38 PM
To see how mysql deals that whitespaces in table names I would suggest you do a dump of the database, using mysqldump. Assuming your database is very big you might want to check the commandline options for mysqldump, to get only the table definitions.

Fine, I tried typing MYSQL dump, it didn't work, what else could I have written?

I stated my problem, all I wanted to know was how to select table names with space in, which are possible, that was solved by Doctor Luz so many thanks to him.

khp
February 22nd, 2005, 09:56 PM
Fine, I tried typing MYSQL dump, it didn't work, what else could I have written?

Are you serious ?, Are you saying it gave you a message, saying 'This does not work' ?.
Or does it not occur to you to, say what message you got ?.
Did you not read my suggestion to check the commandline options for mysqldump ?.

jim_scott
October 6th, 2005, 05:11 AM
Could i maybe ask another question..totally seperate


i have two varchar fields say

field1 varchar(20)
field2 varchar(4)

i want to return a result in the format field1 - field2

my query tries to add the two fields together

select (field1 + ' - ' + field2) as result from table

any help much appreciated

wildfrog
October 6th, 2005, 05:40 AM
i want to return a result in the format field1 - field2
Have you tried this?
select Concat(field1, ' - ', field2) as result from table
- petter

PeejAvery
October 8th, 2005, 11:21 PM
So have you just plain tried just using the column to your advantage. "Percentage mark" must be a column. Just return that column from an array like the following.

<?php

//Replace whatever with number of column where "Percentage mark" is.

$x = whatever;

$query = "SELECT * FROM `assessments`";
$result = mysql_query($query, $connection);
$col = mysql_fetch_array($result, MYSQL_BOTH);

echo $col[$x];

?>

Judoka
October 18th, 2005, 12:34 PM
I stumbled upon this thread while searching for the solution to a similar problem, and while my comment really has nothing to do with this problem's solution I couldn't help but make one.

khd, you have an attitude that I have found on many forums that attract experts - which I believe you probably are. However, you should take a moment and step back to when you were a novice at programming. The tone, attitude, and general abrasiveness of your posts, do nothing but discourage people that are truely trying to learn a little information about a problem that they are having.

Here's a translation of the tone for most of your replies(I've read several), since you seem to lack an semblance of social skills and have NO CLUE as to how rude you sound:

Your actual reply:
Why would you ever do that ?. How was the table even created in the first place ?. Are you sure it's not just an underscore you are somehow failing to read ?

Translated for your pea sized brain:
Geez, why are you so stupid to ask such an idiotic question, can't you just do it right. Can't you even read...MORON?

Did you ever think that Nibinaear may have inherited a database that was poorly created? Might I suggest this type of reply in the future:

1. Here is the solution that I have for you.
2. You may want to consider a design change including - X, Y, Z
no more no less....

I seriously hope that you aren't as rude in person as you are on the forum.

khp
October 18th, 2005, 02:24 PM
khd, you have an attitude that I have found on many forums that attract experts - which I believe you probably are. However, you should take a moment and step back to when you were a novice at programming. The tone, attitude, and general abrasiveness of your posts, do nothing but discourage people that are truely trying to learn a little information about a problem that they are having.

I'am quite aware how my posts might sound to some, and it is very much intentional, I don't come here to help people, or rather I don't come here to solve people's problems.
I come here to help people help them selves, a large portion of the questions asked in these forums could have been answered by the OP, if he or she would put enough effort into it. If the people asking these questions, are ever to become successfull at anything, they have got to learn how to help them selves.

I know some people might be scared away by my attitude, in particular people who constantly cling to others for help. Again this is very much intentional.


Here's a translation of the tone for most of your replies(I've read several), since you seem to lack an semblance of social skills and have NO CLUE as to how rude you sound:

Translated for your pea sized brain:

Talk about the pot that called the kettle black :)

At this point I'am wondering why I even bother do dignify your post with an answer. And I would ask that you would at least show me the courtesy of getting my name right.

Other than that I would like to point out that that the statement you quoted did have a lot of merit, I asked him why he was doing something that is probably done in less than 0.1% of all databases. I gave him a hint as to how he might resolve the problem, and I asked him to double check that he was not mis reading something, you would not believe the number of mistakes has been made because of simple misinterpretation.

dalefish
January 31st, 2008, 08:47 AM
So have you just plain tried just using the column to your advantage. "Percentage mark" must be a column. Just return that column from an array like the following.

<?php

//Replace whatever with number of column where "Percentage mark" is.

$x = whatever;

$query = "SELECT * FROM `assessments`";
$result = mysql_query($query, $connection);
$col = mysql_fetch_array($result, MYSQL_BOTH);

echo $col[$x];

?>




Why is it we always overlook the simple solution (above) to try and track down a complicated fix (most of my programming)? Simple and correct,

Cheers

Dalefish

PeejAvery
January 31st, 2008, 01:10 PM
Welcome to the forums dalefish. Please remember to make your posts more relevant. This thread is over 2 years old.