Sad that a simple select query is taking away my breath. I am creating an alerting tool which can alert when the DB credential expires. The tool can do this on both MSSQL and MySQL. In MySQL I am selecting a column with default value of a column. This query is working if it is added after the *. But throws syntax error if added before the *.
Example:
The below query works.
select * , 'apple' from mysql.user
But the below query errors out.
select 'app' , * from mysql.user
Errors with the below error.
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 mysql.user' at line 1
Please tell me what is the mistake in my query. If you are interested in my actual business case, below is the oracle query that I am trying to find the MySQL equivalent of it. The value of variables {1}, {2} etc will be passed from the C# code.
select
'{0}' as FK_APP_ID,
'{1}' as IP,
'{2}' as ORA_SCHEMA,
username as LoginName,
account_status as IsExpired,
to_date(expiry_date, 'dd-mm-yy') - to_date(SYSDATE, 'dd-mm-yy') as DaysUntilExpiration,
profile as Profile,
expiry_date as ExpiryDate from dba_users
2
Answers
In MySQL, you can’t put a static value before
*
in aSELECT
query, which is causing the syntax error. You can use the query as follows:Appears to have to do with what the manual states here, https://dev.mysql.com/doc/refman/8.4/en/select.html:
So
SELECT 'app', mysql.user.* FROM mysql.user
should work here.