它如何使用别名进行 mysql 查询? [英] How it works a mysql query with alias?

查看:99
本文介绍了它如何使用别名进行 mysql 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个社区的新手,我需要使用从 mysql 数据库获取数据的查询,我有这个查询,但我需要添加一个新表,但我不明白为什么查询有别名,我不知道它是如何工作的,有人可以帮助我吗?这是我的查询:

SELECT ins.matricula, pe.nombres, pe.appaterno, pe.apmaterno, co.calleynum, co.colonia, co.municipio, co.telfijo, co.telcelular, pe.fechanac, pe.sexo, co.email, pe.institucion, tu.tnombres, tu.tappaterno, tu.tapmaterno, tu.direccion, tu.telefono, ins.fechains, ins.niveledu, ins.fechaini, ins.horarioFROM Inscripciones insLEFT JOIN Perfiles peON pe.idperfil=ins.idperfilLEFT JOIN Contactos coON co.idperfil = pe.idperfilLEFT JOIN Tutores tuON tu.matricula = ins.matricula哪里 pe.idperfil='$var'

我已经阅读了 mysql 文档,但我不明白它是如何工作的.

解决方案

在 MySQL 中,可以声明别名"以简化以后的查询.通常这用AS"运算符表示,但也可以在没有AS"的情况下声明 - 如您的示例所示.

在您的示例中:

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

ins 被设置为Inscripciones"表的别名.这允许您在整个查询中使用 ins 而不是输入Inscripciones".这可以在 SELECT 语句中看到.要记住的事情 - SQL 中的别名可以在第一次使用后声明.在您的示例中就是这种情况,其中 SELECT 在您实际将 ins 声明为 Inscripciones 的别名之前获取 ins.matricula.有时这似乎违反直觉,但我保证,如果您稍微尝试一下,它就会有意义.

我发现包含AS"不太含糊——这可能有助于在您阅读/编写 SQL 查询时更有意义.例如:... FROM Inscripciones AS ins

需要明确的是,别名的使用不会改变查询的结果,但可以帮助您编写更清晰的查询,因为您不必每次想使用它时都重新编写表名.

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 query has a alias, I don't know how it works, someone can help me? This is my query:

SELECT ins.matricula, pe.nombres, pe.appaterno, pe.apmaterno, co.calleynum, co.colonia, co.municipio, co.telfijo, co.telcelular, pe.fechanac, pe.sexo, co.email, pe.institucion, tu.tnombres, tu.tappaterno, tu.tapmaterno, tu.direccion, tu.telefono, ins.fechains, ins.niveledu, ins.fechaini, ins.horario 
FROM Inscripciones ins 
LEFT JOIN Perfiles pe 
       ON pe.idperfil=ins.idperfil 
LEFT JOIN Contactos co 
       ON co.idperfil = pe.idperfil 
LEFT JOIN Tutores tu 
       ON tu.matricula = ins.matricula 
WHERE pe.idperfil='$var'

I have read the mysql docs but I can't understand how it works.

解决方案

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.

这篇关于它如何使用别名进行 mysql 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆