mysql-error-1054

Django raw() query, calculated field in WHERE clause

廉价感情. 提交于 2019-12-01 10:17:40
问题 I'm wondering if there are any limitations on syntax of raw() method when using calculated fields. Here is a quick example: Company.objects.raw('''SELECT *,core_location.a + core_location.b as dist FROM core_location,core_company ORDER BY dist''') The above code works as expected (the results are sorted by calculated field 'dist'), but when I add WHERE clause, for example: Company.objects.raw('''SELECT *,core_location.a + core_location.b as dist FROM core_location,core_company WHERE dist<10

MySQL “Unknown Column in On Clause” [duplicate]

拈花ヽ惹草 提交于 2019-12-01 03:01:19
This question already has an answer here: MySQL unknown column in ON clause 3 answers I have the following MySQL query: SELECT posts.id, posts.name, LEFT(posts.content, 400), posts.author, posts.date, users.display_name, GROUP_CONCAT(tags.tag ORDER BY tag_linking.pid ASC SEPARATOR ",") update_tags FROM posts, tag_linking, tags INNER JOIN `users` ON posts.author=users.id; WHERE tag_linking.pid = posts.id AND tags.id = tag_linking.tid ORDER BY posts.date DESC Which, was you can see, connects three tables etc. etc. Anyway, the problem is that it gives an error: ERROR CODE: SQL Error (1054):

Filtering on an alias in mysql

ぐ巨炮叔叔 提交于 2019-12-01 01:13:23
Why doesn't the following query work? Mysql complains about z - can't I use an alias in the WHERE clause? SELECT x + y AS z, t.* FROM t WHERE x = 1 and z = 2 The error that I get is: Error Code : 1054 Unknown column 'z' in 'where clause' NSSec http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined. For example, the following query is illegal: SELECT id, COUNT(*) AS cnt FROM tbl_name WHERE cnt >

MySQL: Unknown column in where clause error

蹲街弑〆低调 提交于 2019-11-30 20:27:46
I have a PHP script and for some reason mysql keeps treating the value to select/insert as a column. Here is an example of my sql query: $query = mysql_query("SELECT * FROM tutorial.users WHERE (uname=`".mysql_real_escape_string($username)."`)") or die(mysql_error()); That turns into: SELECT * FROM tutorial.users WHERE (uname=`test`) The error was: Unknown column 'test' in 'where clause' I have also tried: SELECT * FROM tutorial.users WHERE uname=`test` In MySql, backticks indicate that an indentifier is a column name. (Other RDBMS use brackets or double quotes for this). So your query was,

MySQL: Unknown column in where clause error

允我心安 提交于 2019-11-30 04:43:50
问题 I have a PHP script and for some reason mysql keeps treating the value to select/insert as a column. Here is an example of my sql query: $query = mysql_query("SELECT * FROM tutorial.users WHERE (uname=`".mysql_real_escape_string($username)."`)") or die(mysql_error()); That turns into: SELECT * FROM tutorial.users WHERE (uname=`test`) The error was: Unknown column 'test' in 'where clause' I have also tried: SELECT * FROM tutorial.users WHERE uname=`test` 回答1: In MySql, backticks indicate that

trying to get the number of months

﹥>﹥吖頭↗ 提交于 2019-11-29 12:02:28
membership table membership_startdate (2011-01-12) membership_dueday values like only dates (09,08,07) member_id member table member_id How can I get count of the number of months that the member has paid till now and taking into consideration (membership_dueday)? Suppose if membership_startdate is 2011-01-01 and membership_dueday is 15, the the number of months count up to till now is 5.5 I have tried this code SELECT COUNT(NUMBEROFMONTHS) FROM membership WHERE NUMBEROFMONTHS = PERIOD_DIFF(membership.membership_startdate, CURDATE()); It was giving error like this: Error Code: 1054 Unknown

How to limit results of a LEFT JOIN

我的梦境 提交于 2019-11-29 05:36:26
Take the case of two tables: tbl_product and tbl_transaction . tbl_product lists product details including names and ids while tbl_transaction lists transactions involving the products and includes dates, product-ids, customers etc. I need to display a web-page showing 10 products and for each product, the last 5 transactions. So far, no LEFT JOIN query seems to work and the subquery below would have worked if mysql could allow the tx.product_id=ta.product_id part (fails with Unknown column 'ta.product_id' in 'where clause': [ERROR:1054] ). SELECT ta.product_id, ta.product_name, tb.transaction

#1054 - Unknown column - SQL Query Problem [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-11-28 10:48:48
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Was: Not unique table :: Now: #1054 - Unknown column - can't understand why? After having solved a previous issue with this query, i'm now stuck with getting this error: 1054 - Unknown column 'calendar_events.jobID ' in 'on clause' I can't undertstand why... and the column defiantly exists! Is it something to do with the WHERE blah AND ... section of the query at the bottom? SELECT calendar_events.* , calendar

How to limit results of a LEFT JOIN

三世轮回 提交于 2019-11-27 23:09:58
问题 Take the case of two tables: tbl_product and tbl_transaction . tbl_product lists product details including names and ids while tbl_transaction lists transactions involving the products and includes dates, product-ids, customers etc. I need to display a web-page showing 10 products and for each product, the last 5 transactions. So far, no LEFT JOIN query seems to work and the subquery below would have worked if mysql could allow the tx.product_id=ta.product_id part (fails with Unknown column

Can I reuse a calculated field in a SELECT query?

喜你入骨 提交于 2019-11-27 03:03:27
Is there a way to reuse a calculated field within a mysql statement. I get the error "unknown column total_sale" for: SELECT s.f1 + s.f2 as total_sale, s.f1 / total_sale as f1_percent FROM sales s or do I have to repeat the calculation, which would make for a very long SQL statement if I added all the calculations I need. SELECT s.f1 + s.f2 as total_sale, s.f1 / (s.f1 + s.f2) as f1_percent FROM sales s of course I can do all the calculations in my php program. rzetterberg Yes, you can reuse variables. This is how you do it: SELECT @total_sale := s.f1 + s.f2 as total_sale, s.f1 / @total_sale as