简体   繁体   English

作为连接的结果获取PHP MySQL PDO列表名

[英]Get PHP MySQL PDO column table name as a result of a join

I have fields that have the same name in different tables that I'm joining. 我在加入的不同表中有相同名称的字段。 Such as ticket.status , user.status and transaction.status . 例如ticket.statususer.statustransaction.status At the moment the query returns just status . 目前,查询只返回status

How can I get the table name in such a way that it stops similar field names from overwriting and so I can tell the difference between the fields. 如何获取表名以防止类似的字段名称被覆盖,因此我可以区分字段。

Simply put: 简单的说:

$data = array($eventId);
$statement = $this->db->prepare("SELECT * FROM ticket, user, transaction 
                        WHERE ticket.eventId = ? 
                        AND ticket.userId = user.userId
                        AND ticket.transactionId = transaction.transactionId");

$statement->execute($data); 
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);

In my research I've found the constant PDO::ATTR_FETCH_TABLE_NAMES that looks like it could help, but I do not know how to implement ( I assume through $statement->setAttribute(); somehow). 在我的研究中,我发现了常量PDO::ATTR_FETCH_TABLE_NAMES看起来可能会有所帮助,但我不知道如何实现(我假设通过$statement->setAttribute();不知何故)。

I also have concerns that it will not work, as the PHP documentation mentions it is dependent on the driver. 我也担心它不起作用,因为PHP文档提到它依赖于驱动程序。

Thanks 谢谢

Just add new aliases to your select statements 只需在select语句中添加新别名即可

$statement = $this->db->prepare("
    SELECT *, ticket.status AS ticket_status, user.status AS user_status, transaction.status AS transaction_status 
    FROM ticket, user, transaction 
    WHERE ticket.eventId = ? 
    AND ticket.userId = user.userId
    AND ticket.transactionId = transaction.transactionId
");

Then you can do 那你可以做

$rows[0]['user_status'];
$rows[0]['ticket_status'];
$rows[0]['transaction_status'];

If you are really concern by performance, the quantity of data returned will be greater so instead of adding new aliases you can select every single columns and while you do so put an alias on the status column. 如果您真的关心性能,那么返回的数据量会更大,因此您可以选择每个列而不是添加新别名,而在这样做时,会在status列上添加别名。

Why not change your to actually join instead: 为什么不改变你的实际加入:

SELECT 
t.status as ticket_status, u.status as user_status, tr.status as trans_status
FROM 
ticket as t
inner join user as u on t.userId = u.userId
inner join transaction as tr on t.transactionId = tr.transactionId
where
t.eventId = ?

You don't even need to cast the tables using as something but I find it's neater. 你甚至不需要把桌子as something使用as something但我发现它更整洁。

Note, its the casting of the columns that will actually fix this issue, not the join method. 注意,它是实际修复此问题的列的转换,而不是连接方法。

The most obvious comment is "don't do it, that's why aliases exist". 最明显的评论是“不要这样做,这就是为什么存在别名”。 But there's still a good underlying question: does MySQL send information about where a result-set column comes from (table, view or calculated)? 但是仍然存在一个很好的基本问题:MySQL是否发送了关于结果集列来自何处(表,视图或计算)的信息?

Apparently, it does, since the PDOStatement object has an experimental method called getColumnMeta() . 显然,确实如此,因为PDOStatement对象有一个名为getColumnMeta()的实验方法。 I've been testing and it returns an associative array where the table key 我一直在测试,它返回table键的关联数组

  • contains the source table if column comes from a table or view 如果列来自表或视图,则包含源表
  • is an empty string if the column is calculated 如果计算列,则为空字符串

Of course, I'd stick to aliases anyway. 当然,无论如何我都会坚持使用别名。 Being able to use associative arrays is a killer feature for me. 能够使用关联数组对我来说是一个杀手级的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM