HQL Query - issue in where clause by using alias name - mysql

I got a exception in my where clause. I have the following HQL-Query:
SELECT a.addressType as myowncolumn
FROM Address a
WHERE myowncolumn = 1
I got the following error message:
Unknown column "myowncolumn" in where clause
If I remove the where clause the query is valid.
What´s wrong?
Could you help me, please.

Your entity "must know" (have the links through fields) about myowncolumn. For example, if Address has reference to City and them to Country, then this hql query will be correct:
select adr from Address a where a.city.country.name='UK';

That doesn't work even in plain SQL, and you are right in your comment that SQL is validated from inside out / right to left. That's why myowncolumn isn't recognized.

Related

Getting SQL Error: "SELECT" is not valid at this position for this server version, expecting '(' with

I have been trying out the following query in MySQL Workbench:
SELECT NAME,LEAD,OUTCOME,COUNT(*) AS NUMBER_OUTCOME
FROM OUTCOMES_BY_USER
ORDER by NAME,LEAD,OUTCOME ASC;
However I am getting this error:
"SELECT" is not valid at this position for this server version, expecting '(' with
I have tried to find out where the error is coming from by taking away parts of the query and seeing where it breaks, it seems to work when I try:
SELECT NAME
FROM OUTCOMES_BY_USER;
However when I add in another column (as shown below) I start getting the same error:
SELECT NAME, LEAD
FROM OUTCOMES_BY_USER;
I am really not sure how to get around this error, I was trying this query in sqlfiddle and it worked fine, however my sqlfiddle suddenly stopped working and the website just flat out wont build schemas for me anymore. so I tried it out on a my universities MySQL server and have been getting this error. Please help!
LEAD() is a MySQL function, hence it is a reserved word. You can add backticks around reserved table or column names to bypass this error :
SELECT NAME,`LEAD`,OUTCOME,COUNT(*) AS NUMBER_OUTCOME
FROM OUTCOMES_BY_USER
ORDER by NAME,`LEAD`,OUTCOME ASC;

Putting in line comments in sql view for Access 2016

I am trying to figure out how to put in line comments in Access.
I have seen the below post and have been trying to put through the solution from Dan which includes the WHERE Clause however it comes up with a "Syntax error (missing operator) in query expression AND "Comment FYI, This is a comment"<>"".
Script I have put through below for reference:
SELECT prod_name
FROM products
WHERE
AND "Comment: FYI, This is a comment"<>"";
How do you comment an MS-access Query?
Thanks!
The answer assumes that there is something else in the WHERE clause.
If there isn't, you would simply do:
SELECT prod_name
FROM products
WHERE "Comment: FYI, This is a comment"<>"";

Select * from Select

Its very weird situation I know, nut I have got myself into it somehow. I have to connect to some other system service by passing some parameters in url.
In their service they are creating some query using parameter I pass.
For my case I have to pass 'Select' as a parameter name which is actually some class name on their side. So they end up in creating query as Select * from select
and some condition.
On execution I am getting error response as:
'There was a syntax error in a SQL query or filter expression at line
1, position 186. Saw \"Select\" but expected
'..SQL: \"SELECT col1, col2 FROM Select AS D where
some condition.
Can somebody help me on this.
Since Select is reserved word, you have to escape it by enclosing in backticks characters in order for MySQL to process your query:
select * from `select`
Its recommended not to use MySQL reserved keywords.. but if its necessary there is a solution..
Use this, it will work for you :
select * from yourdatabasename.select

Why is my query wrong?

before i use alias for table i get the error:
: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous
Then i used aliases and i get this error:
unknown index a
I am trying to get a list of category name ( dependant to a translation) and the associated category id which is unique. Since i need to put them in a select, i see that i should use the lists.
$categorie= DB::table('cat as a')
->join('campo_cat as c','c.id_cat','=','a.id')
->join('campo as d','d.id','=','c.id_campo')
->join('cat_nome as nome','nome.id_cat','=','a.id')
->join('lingua','nome.id_lingua','=','lingua.id')
->where('lingua.lingua','=','it-IT')
->groupby('nome.nome')
->lists('nome.nome','a.id');
The best way to debug your query is to look at the raw query Laravel generates and trying to run this raw query in your favorite SQL tool (Navicat, MySQL cli tool...), so you can dump it to log using:
DB::listen(function($sql, $bindings, $time) {
Log::info($sql);
Log::info($bindings);
});
Doing that with yours I could see at least one problem:
->where('lingua.lingua','=','it-IT')
Must be changed to
->where('lingua.lingua','=',"'it-IT'")
As #jmail said, you didn't really describe the problem very well, just what you ended up doing to get around (part of) it. However, if I read your question right you're saying that originally you did it without all the aliases you got the 'ambiguous' error.
So let me explain that first: this would happen, because there are many parts of that query that use id rather than a qualified table`.`id.
if you think about it, without aliases you query looks a bit like this: SELECT * FROM `cat` JOIN `campo_cat` ON `id_cat` = `id` JOIN `campo` ON `id` = `id_campo`; and suddenly, MySQL doesn't know to which table all these id columns refer. So to get around that all you need to do is namespace your fields (i.e. use ... JOIN `campo` ON `campo`.`id` = `campo_cat`.`id_campo`...). In your case you've gone one step further and aliased your tables. This certianly makes the query a little simpler, though you don't need to actually do it.
So on to your next issue - this will be a Laravel error. And presumably happening because your key column from lists($valueColumn, $keyColumn) isn't found in the results. This is because you're referring to the cat.id column (okay in your aliased case a.id) in part of the code that's no longer in MySQL - the lists() method is actually run in PHP after Laravel gets the results from the database. As such, there's no such column called a.id. It's likely it'll be called id, but because you don't request it specifically, you may find that the ambiguous issue is back. My suggestion would be to select it specifically and alias the column. Try something like the below:
$categories = DB::table('cat as a')
->join('campo_cat as c','c.id_cat','=','a.id')
->join('campo as d','d.id','=','c.id_campo')
->join('cat_nome as nome','nome.id_cat','=','a.id')
->join('lingua','nome.id_lingua','=','lingua.id')
->where('lingua.lingua','=','it-IT')
->groupby('nome.nome')
->select('nome.nome as nome_nome','a.id as a_id') // here we alias `.id as a_id
->lists('nome_nome','a_id'); // here we refer to the actual columns
It may not work perfectly (I don't use ->select() so don't know whether you pass an array or multiple parameters, also you may need DB::raw() wrapping each one in order to do the aliasing) but hopefully you get my meaning and can get it working.

Unable to get mysql to recognise query using in statement

I've been attempting to adjust a mysql SELECT statement based on input from checkboxes. The php code collects the ticked checkboxes into an array, implodes them into a comma-separated list and then runs the query using an in statement (as was detailed here).
The query generated comes out as SELECT * FROM events WHERE Discipline IN (SJ,OTHER) which is a correctly formatted query as far as I can tell.
This shows up as an invalid query when run from the php code. When I run the query using phpmyadmin, I receive this message:
#1054 - Unknown column 'SJ' in 'where clause'
I was wondering if anyone could tell my why that query is generating the error?
Try putting quotes around the values in the IN clause:
SELECT * FROM events WHERE Discipline IN ('SJ','OTHER')
If SJ and OTHER are string literal use:
SELECT * FROM events WHERE Discipline IN ('SJ','OTHER')