Johan Beijar Posted March 25, 2009 Share Posted March 25, 2009 Hi, After some extensive trouble-shooting I came to my senses and understood that I need help. I'm trying to execute the SQL-question below and I get the errormessage above it and I do not understand why. I run the query in MySQL Admin and it works like a charm. There is no column "test1" but it should select the row where the username is "test1". It seams to be some issue with `plogger_collections`.`username` = test1 but that's all that I know. test1 is actually a varchar variable. Does anyone have a clue why it is not working and what I should do? Thank you, /Johan Unknown column 'test1' in 'where clause' SELECT name, description, path, id, thumbnail_id, username FROM `plogger_collections` WHERE `plogger_collections`.`username` = test1 and `id` IN (-1,2,4) ORDER BY id DESC LIMIT 0, 20 Quote Link to comment https://forums.phpfreaks.com/topic/151092-sql-query-challenge/ Share on other sites More sharing options...
steelaz Posted March 25, 2009 Share Posted March 25, 2009 You should surround your value fields in single quotes. SELECT `name`, `description`, `path`, `id`, `thumbnail_id`, `username` FROM `plogger_collections` WHERE `plogger_collections`.`username` = 'test1' and `id` IN (-1,2,4) ORDER BY `id` DESC LIMIT 0, 20 Quote Link to comment https://forums.phpfreaks.com/topic/151092-sql-query-challenge/#findComment-793740 Share on other sites More sharing options...
Maq Posted March 25, 2009 Share Posted March 25, 2009 This should work, you had two sets of backticks on username, you also used plogger_collections.username when you only had to use username, and like steelaz said, any value that's not an integer should be surrounded with single quotes. See if this works. $sql = "SELECT name, description, path, id, thumbnail_id, username FROM `plogger_collections` WHERE `username` = 'test1' AND `id` IN (-1,2,4) ORDER BY id DESC LIMIT 0, 20"; NOTE: You really only need backticks for column names that are reserved words. Quote Link to comment https://forums.phpfreaks.com/topic/151092-sql-query-challenge/#findComment-793813 Share on other sites More sharing options...
Johan Beijar Posted March 25, 2009 Author Share Posted March 25, 2009 Thank you Maq! That did it, i'm a happy man and will get a beuty-sleep tonight. Thanx! /Johan Quote Link to comment https://forums.phpfreaks.com/topic/151092-sql-query-challenge/#findComment-793839 Share on other sites More sharing options...
Maq Posted March 25, 2009 Share Posted March 25, 2009 Thank you Maq! That did it, i'm a happy man and will get a beuty-sleep tonight. Thanx! /Johan Sure, you only had a few minor errors. The main thing was that when you select FROM a single table you don't need to specify that table before the column name. SQL thought you were referring to 2 tables because you had 2 sets of backticks. Mark as [sOLVED] please. Quote Link to comment https://forums.phpfreaks.com/topic/151092-sql-query-challenge/#findComment-793842 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.