SQL Query Differences between MS Access and MySQL - mysql

I'm having trouble converting a MS Access Query into a MySQL view. It seems that in Access, you're able to use a field that's created within the same select statement later on in the select statement. Whereas, in MySQL, that seems to not be the case.
e.g. Access Generated Code
SELECT IIf([Bonus]>0,[Base Salary],0) AS [BE Base], [BE Base]+[Bonus] AS [BE TC]
FROM titles_tbl INNER JOIN wage_tbl ON titles_tbl.Job_Title = wage_tbl.[Standard Title]
WHERE (((titles_tbl.Mod_Code)="3B") AND ((wage_tbl.Bonus)>0));
vs MySQL Converted Code
select if(`bonus`>0,`base salary`,0) as `be base`, `be base`+`bonus` as `be tc`
from titles_tbl inner join wage_tbl on titles_tbl.job_title = wage_tbl.`standard title`
where (((titles_tbl.mod_code)="3b") and ((wage_tbl.bonus)>0));
When I put this Access-generated SQL code into MySQL (after converting it appropriately), I get an error saying
10:17:07 Error Code: 1054. Unknown column 'be base' in 'field list' 0.062 sec
To me, it seems like MySQL can't use a new column in the same select statement that it's generated in.
Does anyone know whether that's the case, or what the real problem might be (as well as a solution)? Thank you in advance!

Your MySQL code should be
select if(`bonus`>0,`base salary`,0) as `be base`,
((SELECT `be base`) + `bonus`) as `be tc`
from ...
Firstly, if you use spaces in aliases you should surround the alias with ' (quote) or ` (backticks)
Secondly, if you want to refer, in a SELECT, to an alias of the same SELECT, you have to (SELECT `the_alias`)
And thirdly, in the subselect it is mandatory to wrap the aliased column with ` (backticks), simple quotes won't work there.

Related

SELECT statement with spaces between tables?

I have the following SQL statement:
SELECT pvov.id, pvov.value
FROM ci_product_variant_option_values pvov, ci_product_variant_options pvo
WHERE pvov.product_variant_option_id=pvo.id
AND pvo.product_id='12345'
Now, what I don't understand is what should ci_product_variant_option_values pvov mean, as it has neither any commas or dots. I know that ci_product_variant_option_values is a table in the database, however pvov (probably an abreviation for product variant option values) has no occurrence in the db.
Can someone please explain me the statement?
pvov is the table alias.
Same as saying:
ci_product_variant_option_values AS pvov
Aliases make code cleaner, instead of having to use ci_product_variant_option_values.id you can simply use pvov.id
I'd also suggest moving away from the deprecated syntax by using explicit JOIN criteria:
SELECT pvov.id, pvov.value
FROM ci_product_variant_option_values pvov
JOIN ci_product_variant_options pvo
ON pvov.product_variant_option_id=pvo.id
WHERE ....
pvov is an alias for ci_product_variant_option_values.

Select from View using Where clause to refer to another database - Unknown Column error

I am trying to select certain values from a view that I created. The statement is below:
SELECT * FROM dashboard.team
WHERE ac2012.acx_users.id = 1;
As you can see, there are 2 databases being referenced here:
dashboard database, team table
ac2012 database, acx_users.id table
ac2012.acx_users.id is the regular expression in the original Create View statement, I'm using that since of course I can't use an ALIAS in a Where clause... however, this is showing an error:
Error Code 1054: Unknown column 'ac2012.acx_users.id' in 'where clause'
I'm not sure how to get this to work, because I need to reference the other database in this case, but it's not recognizing the database. Any tips would be appreciated.
Since you're selecting from a view, the underlying databases aren't visible anymore. You only see what the view presents, as part of the database which the view lives in, so try WHERE acx_users.id = 1, or whatever you've aliased that field to in the view definition.
SELECT * FROM dashboard.team
LEFT OUTER JOIN ac2012 ON ac2012.CommonColumnName=dashboard.CommonColumnName
WHERE ac2012.acx_users.id = 1;
======================
Please replace by original column name ...

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')

MySQL developer here -- Nesting with select * finicky in Oracle 10g?

I'm writing a simple diagnostic query then attempting to execute it in the Oracle 10g SQL Scratchpad. EDIT: It will not be used in code. I'm nesting a simple "Select *" and it's giving me errors.
In the SQL Scratchpad for Oracle 10g Enterprise Manager Console, this statement runs fine.
SELECT * FROM v$session sess, v$sql sql WHERE sql.sql_id(+) = sess.sql_id and sql.sql_text <> ' '
If I try to wrap that up in Select * from () tb2 I get an error, "ORA-00918: Column Ambiguously Defined". I didn't think that could ever happen with this kind of statement so I am a bit confused.
select * from
(SELECT * FROM v$session sess, v$sql sql WHERE sql.sql_id(+) = sess.sql_id and sql.sql_text <> ' ')
tb2
You should always be able to select * from the result set of another select * statement using this structure as far as I'm aware... right?
Is Oracle/10g/the scratchpad trying to force me to accept a certain syntactic structure to prevent excessive nesting? Is this a bug in scratchpad or something about how oracle works?
When Oracle parses a SELECT *, it expands it out to an actual list of the columns to be selected. Since your inline view contains two columns named SQL_ID, this results in an ambiguous reference.
Interestingly, using ANSI join syntax seems to cause it to alias the duplicate column names automatically, and therefore avoids the error.
select * from
(select * from v$session sess left outer join v$sql sql on sql.sql_id=sess.sql_id and sql.sql_text <> ' ')
Incidentally, it's not clear to me why you chose that condition on sql_text. I don't expect that column would ever contain a single space. Are you really trying to filter out NULLs? If so, why use an outer join at all?
One of the general rules of thumbs at my place of employment is that SELECT * is never allowed. Explicitly define what columns you need; not only is it more readable, but less likely to have issues down the road

Unknown column in 'field list' error on MySQL Update query

I keep getting MySQL error #1054, when trying to perform this update query:
UPDATE MASTER_USER_PROFILE, TRAN_USER_BRANCH
SET MASTER_USER_PROFILE.fellow=`y`
WHERE MASTER_USER_PROFILE.USER_ID = TRAN_USER_BRANCH.USER_ID
AND TRAN_USER_BRANCH.BRANCH_ID = 17
It's probably some syntax error, but I've tried using an inner join instead and other alterations, but I keep getting the same message:
Unknown column 'y' in 'field list'
Try using different quotes for "y" as the identifier quote character is the backtick (`). Otherwise MySQL "thinks" that you point to a column named "y".
See also MySQL 8 Documentation
Please use double-/single quotes for values, strings, etc.
Use backticks for column-names only.
Enclose any string to be passed to the MySQL server inside single quotes, e.g.:
$name = "my name"
$query = " INSERT INTO mytable VALUES ( 1 , '$name') "
Note that although the query is enclosed between double quotes, you must enclose any string in single quotes.
You might check your choice of quotes (use double-/ single quotes for values, strings, etc and backticks for column-names).
Since you only want to update the table master_user_profile I'd recommend a nested query:
UPDATE
master_user_profile
SET
master_user_profile.fellow = 'y'
WHERE
master_user_profile.user_id IN (
SELECT tran_user_branch.user_id
FROM tran_user_branch WHERE tran_user_branch.branch_id = 17);
Just sharing my experience on this. I was having this same issue. The insert or update statement is correct. And I also checked the encoding. The column does exist.
Then! I found out that I was referencing the column in my Trigger.
You should also check your trigger see if any script is referencing the column you are having the problem with.
In my case, it was caused by an unseen trailing space at the end of the column name. Just check if you really use "y" or "y " instead.
While working on a .Net app build with EF code first, I got this error message when trying to apply my migration where I had a Sql("UPDATE tableName SET columnName = value"); statement.
Turns out I misspelled the columnName.
If it is hibernate and JPA. check your referred table name and columns might be a mismatch
Just sharing my experience on this. I was having this same issue. My query was like:
select table1.column2 from table1
However, table1 did not have column2 column.
In my case, the Hibernate was looking for columns in a snake case, like create_date, while the columns in the DB were in the camel case, e.g., createDate.
Adding
spring:
jpa:
hibernate:
naming: # must tell spring/jpa/hibernate to use the column names as specified, not snake case
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
to the application.ymlhelped fix the problem.
In my case, I used a custom table alias for the FROM table, but I used the default table alias (MyTable) in the field list instead of the custom table alias (t1). For example, I needed to change this...
mysql> SELECT MyTable.`id` FROM `MyTable` t1;
...to this...
mysql> SELECT t1.`id` FROM `MyTable` t1;
In my case I had misspelled the column name in the table's trigger. Took me a while to connect the error message with the cause of it.
I too got the same error, problem in my case is I included the column name in GROUP BY clause and it caused this error. So removed the column from GROUP BY clause and it worked!!!
I got this error when using GroupBy via LINQ on a MySQL database. The problem was that the anonymous object property that was being used by GroupBy did not match the database column name. Fixed by renaming anonymous property name to match the column name.
.Select(f => new
{
ThisPropertyNameNeedsToMatchYourColumnName = f.SomeName
})
.GroupBy(t => t.ThisPropertyNameNeedsToMatchYourColumnName);
A query like this will also cause the error:
SELECT table1.id FROM table2
Where the table is specified in column select and not included in the from clause.