skip to Main Content

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


  1. In MySQL, you can’t put a static value before * in a SELECT query, which is causing the syntax error. You can use the query as follows:

    SELECT *, 'app' AS static_value FROM mysql.user;
    
    Login or Signup to reply.
  2. Appears to have to do with what the manual states here, https://dev.mysql.com/doc/refman/8.4/en/select.html:

    Use of an unqualified * with other items in the select list may produce a parse error. For example:
    SELECT id, * FROM t1
    To avoid this problem, use a qualified tbl_name.* reference:
    SELECT id, t1.* FROM t1

    So SELECT 'app', mysql.user.* FROM mysql.user should work here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search