I am trying to write what should ostensibly, be relative easy SQL queries - yet, I cant seeem to get them to work.
Is it possible to write a query that looks something like this:
select t.name
from (select *
from mystoredproc(1,2,3) as t)
where t.name = 'foobar'
Two problems with the above query:
1) First off all, the statement above dosent work. My db engine (mySQL) complains:
ERROR 1054 (42S22): Unknown column
't.name' in 'field list'
2) I want to use the returned table t
in a SELF JOIN. However, I dont want
to have to call mystoredproc(...)
again, because it is a VERY
expensive call.
Any one knows how to fix these problems?
BTW, even though I am using mySQL (for now), I would prefer if any proffered SQL snippet was db agnostic (i.e. ANSI SQL)
Replace the ) as t) with ) t, as in
select t.name from (select * from mystoredproc(1,2,3)) t where t.name = 'foobar'
For your second problem: Feed the result from mystoredproc into a temporary table and SELECT from it, then you can make a self join without hassles.
Related
SELECT CASE
WHEN col1_alias='' THEN 'empty value'
ELSE 'has value'
END AS result,
(/* a complicated mysql SELECT statement */) AS col_alias
FROM my_table;
The above MySQL query gives me Unknown column 'col_alias' in 'field list' error.
Is it possible to generate the result column based on the value of col?
I don't want to write the complicated MySQL SELECT statement for the second time.
============================== EDIT ================================
Sorry, I forgot to mention that my real situation is more complicated than the query pasted above.
My real query contains JOIN and GROUP BY. Like this:
SELECT
my_table.id AS id,
CASE
WHEN col1_alias='' THEN 'empty value'
ELSE 'has value'
END AS result,
(/* a complicated mysql SELECT statement */) AS col_alias,
another_table.name AS name
FROM my_table
LEFT JOIN another_table
ON
`my_table`.`id` = `another_table`.`id`
GROUP BY
`another_table`.`name`;
Is it possible to avoid Unknown column 'col_alias' in 'field list' in this situation?
I think I might have to write part of the query results to a temporary table. Then write a second query that runs against the original and the temporary table.
However, I still wish that I can use only one query to accomplish the goal.
What is the difference between these two MySQL statements?
Works:
select *, count(mycol) c from mytable group by mycol;
Doesn't work:
select count(mycol) c, * from mytable group by mycol;
The first statement works as I'd expect, while the second one gives me a syntax error. Why does the order matter?
I'm having trouble finding an answer from Google, because I'm not entirely sure if I'm asking the question correctly.
Edit:
Here's the sanitized error message. I'm using MySQL Workbench, if that's relevant.
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* from mytable group by id' at line 1
Just alias the table and the syntax error will go away.
select count(t.id) c, t.* from mytable t group by id;
See this db fiddle.
It looks like MySQL allows bare (unqualified) * only as immediatly following SELECT. The following query also raises a syntax error :
select 1, * from mytable t;
The documentation prevents against using bare * combined with other items in the SELECT list :
A select list consisting only of a single unqualified * can be used as shorthand to select all columns from all tables.
Use of an unqualified * with other items in the select list may produce a parse error. To avoid this problem, use a qualified tbl_name.* reference.
I have a problem with Aliased Columns in MySQL!
My Query:
SELECT Price AS Pr, (Pr*10/100) FROM MyTable;
MySQL WorkBench Error: UnKnown Column 'Pr' in Field List !!!
I tested my query in W3Schools with no error !
I tested my query in W3Schools with no error!
This doesn't prove that your query is valid.
You can only use aliases in GROUP BY, ORDER BY or HAVING clauses. Your usage variant is not allowed, because the value of alias is not known when MySQL is selecting the 2-nd column.
I've got a suspicion that W3Schools uses MS Access to run user queries, and MS Access does allow such atrocity as referencing column aliases in a SELECT clause that are defined in the same SELECT clause.
The standard doesn't allow this and MySQL does follow standard in this particular case.
As for solution to your problem, I can see two options.
The more generic solution, which would run in probably any SQL product, would be to use a derived table:
SELECT
Pr,
(Pr * 10 / 100) AS SomethingElse
FROM
(
SELECT
SomeComplexExpression AS Pr
FROM MyTable
) AS sub
;
The other option would be to use a variable, which is MySQL-specific:
SELECT
#Pr := SomeComplexExpression AS Pr,
(#Pr * 10 / 100) AS SomethingElse
FROM MyTable
;
Finally, if you need to test/demonstrate if something can/cannot work in MySQL, I'd recommend using SQL Fiddle.
#1054 - Unknown column 'default_ps_products.manufacturer_id' in 'order clause'
Why am I getting the above error with the statement below it works fine without the p in the statement and I am not using an order clause?
SELECT * FROM `default_ps_products` p WHERE p.`manufacturer_id` = 2
To solve this use SELECT p.* FROM instead of SELECT * FROM.
The reason is that phpMyAdmin is adding an ORDER BY to your query for the first column in the results grid. Because of the alias, the code that does this fails.
This issue reproduces on phpMyAdmin 4.0.6. I don't know the status on the latest 4.2.5
Since you posted a partial query this wasn't obvious from the start, but your full query makes it clear;
SELECT *
FROM default_ps_products
WHERE manufacturer_id=2
ORDER BY `default_ps_products`.`manufacturer_id` ASC
LIMIT 0, 30
When you add an alias to default_ps_products table in the select, you can't selectively use the alias only in the WHERE clause, you'll also need to change the ORDER BY to use the same alias. The full query should in other words be;
SELECT *
FROM default_ps_products p
WHERE p.manufacturer_id=2
ORDER BY p.`manufacturer_id` ASC
LIMIT 0, 30
Open your phpmyadmin. Click on your selected database. Now you have a list of all tables on right side. Click on structure of default_ps_products table. Now you see a structure of it. Now Click on SQL tab and execute query as 'SELECT * FROM default_ps_products ORDER BY '.
Once you execute this query, Now resolve your problem.
Your query is fine. there is not any error when i run this query. there is nothing wrong with query.
SELECT * FROM default_ps_products AS p WHERE p.manufacturer_id = 2
it working fine.:)
when use #Query(nativeQuery = true), should use underline format ,just like this "Sort.by(Sort.Direction.DESC, "update_time")))", else should use camel properties in entity ,like this "Sort.by(Sort.Direction.DESC, "updateTime")))"
phpmyadmin started showing this error suddenly on one table. only fix was to rename column id to id2 then back. even deleting the table and making new copy didn't help
i am trying to run a query like this
SELECT a, b , c, (SELECT INNULL(x,y)) as mycol WHERE mycol < 400 ;
BUt it gives the error
#1054 - Unknown column 'mycol' in 'where clause'
What would be the right way to do this?
Thanks.
It's so in MS SQL, so I assume, that the same problem is in MySQL.
Try to change WHERE to HAVING. WHERE clause doesn't see your renamed columns.
HAVING is working the same way as WHERE, like (mycol < 400 AND a > 5).
GROUP BY should be before HAVING.
Check the examples in the link.
http://www.databasejournal.com/features/mysql/article.php/3469351/The-HAVING-and-GROUP-BY-SQL-clauses.htm
#hgulyan I doubt your answer. It is not the renaming that prevents one from using WHERE clause, rather it is the subquery. So lets say I have a query:
SELECT id as ID FROM user WHERE ID > 10;
This is going to work perfectly fine.
Now lets say I have one more query:
SELECT name, (SELECT id FROM user_detail WHERE user_id = 20) as ID FROM user WHERE ID > 19;
This particular query will produce error as:
Unknown column ID
So, it's about using subquery and column aliases and not just column aliases.
Thus in this case you will have to use HAVING instead of WHERE.