How it works a mysql query with alias?

前端 未结 4 745
旧时难觅i
旧时难觅i 2021-01-28 10:17

I\'m new in this comunnity and I need to work with a query that get data from a mysql database, I have this query, but I need to add a new table and I don\'t understand why the

4条回答
  •  面向向阳花
    2021-01-28 10:26

    In MySQL, an "alias" can be declared to simplify the query later. Typically this is denoted with the "AS" operator, but can also be declared without "AS" - as in your example.

    In your example:

    SELECT ins.matricula, {...}
    FROM Inscripciones ins {...}
    

    The ins is set as an alias for the "Inscripciones" table. This allows you to use ins throughout the query rather than typing out "Inscripciones." This can be seen in the SELECT statement. Something to keep in mind - aliases in SQL can be declared after they're first used. This is the case in your example, where SELECT gets ins.matricula before you've actually declared ins as the alias for Inscripciones. Sometimes this seems counter intuitive, but I promise it will make sense if you experiment with it a bit.

    I find it less ambiguous to include the "AS" - which might help it make more sense as you're reading/writing the SQL query. ex: ... FROM Inscripciones AS ins

    To be clear, the use of the alias doesn't change the outcome of your query, but helps you write cleaner queries because you don't have to re-write the tablename every time you want to use it.

提交回复
热议问题