mysql - How do I include table names in a MySQL query?
问题描述
This seems like it would be a simple setting, but I cannot find it. I do inner join queries on tables that have similar column names. It would be nice to include the table name in the query results, so the people receiving the data can differentiate more easily. For example:
Table1: id name timestamp
Table2: id name timestamp table1_id
Table3: id name timestamp table2_id
Then I tie it all together with a query:
select * from table1
inner join table2 on table1.id=table2.table1_id
inner join on table2.id=table3.table2_id;
The results have similar column header names:
id name timestamp id name timestamp table1_id id name timestamp table2_id
It's hard to tell the data apart. Of course the example query is short and silly and pointless. If I do an actual query with all the data it get more complicated. Couldn't the column header name include the table name?
table1.id table1.name table1.timestamp table2.id table2.name table2.timestamp table2.table1_id table3.id table3.name table3.timestamp table3.table2_id
解决方案
Instead of writing
select * from
you can write
select table1.id as table1_id,
and do the same for the other columns so that the results set would show you the names you give yourself for each column
推荐阅读
- c++ - 可以将类函数称为无效吗?
- c# - 从给定的代码中,我只显示 1 个重复次数最多的数字,即 6、6、6、6,但我也想带出第二个数字:3、3、3、3,
- c# - 类的多个实例
- r - 一个 y 轴上的 2 个图 - 错误:提供给连续刻度的离散值
- javascript - 带参数的函数不在 Javascript AddEventListener 中运行
- azure - 可以使用 ARM 模板在 MSSQL 中创建其他用户和角色吗?
- c - Token 忽略String中的一些元素
- javascript - 尝试到达定义的路线时出现 404 错误
- google-sheets - 查询显示 Condition Google Sheets 错误
- c++ - 在 Visual Studio 中调试 QML 无法连接到套接字错误