简体   繁体   English

SQL / PHP-将2个表与其他表中的数据连接起来

[英]SQL/PHP - Join 2 Tables with data from other table

This is the query for SQL statement 这是对SQL语句的查询

UPDATE TrendingEvents
    SET Name = ?, Image = ?, Date = ?, EventCategory = ?
    WHERE ID = ?;')

I would like to access the ID of the other table within my TrendingEvents table. 我想访问TrendingEvents表中另一个表的ID。 This is the example I've done although it doesn't fully work; 这是我做过的示例,尽管它不能完全起作用。

UPDATE TrendingEvents INNER JOIN
       Events AS eID
       ON TrendingEvents.ID = Events.ID
 SET Name = ?, Image = ?, Date = ?, EventCategory = ?
 WHERE eID = ?;')

I would like to update the TrendingEvents table with the ID column from Events table. 我想使用事件表中的ID列更新TrendingEvents表。

The error I'm getting from my statement is 我从陈述中得到的错误是

Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'Name' 

Which just shows that I've poorly designed my query. 这只是表明我对查询的设计不佳。

Both tables seem to contain a column called Name. 这两个表似乎都包含一个名为Name的列。 You need to properly prefix the fields with the table name, like : 您需要使用表名正确为字段加上前缀,例如:

UPDATE 
    TrendingEvents AS t
    INNER JOIN Events AS e
        ON t.ID = e.ID 
    SET 
        t.Name = ?, 
        t.Image = ?, 
        t.Date = ?, 
        t.EventCategory = ? 
    WHERE e.eID = ?

You have misused the table alias, so your query should not even compile. 您已经滥用了表别名,因此查询甚至不应编译。 Try this: 尝试这个:

UPDATE TrendingEvents te INNER JOIN
       Events e
       ON te.ID = e.ID
 SET te.Name = ?,
     te.Image = ?,
     te.Date = ?,
     te.EventCategory = ?
 WHERE e.eID = ?;

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

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