MySQL: Determine Table's Primary Key Dynamically

前端 未结 5 2145
别那么骄傲
别那么骄傲 2020-12-03 14:06

I\'m, generating a SQL query like this in PHP:

$sql = sprintf(\"UPDATE %s SET %s = %s WHERE %s = %s\", ...);

Since almost every part of thi

5条回答
  •  天命终不由人
    2020-12-03 14:23

    Based on @jake-sully and @lukmdo answers, making a merge of their code, I finished with the following snippet:

    SELECT `COLUMN_NAME`
    FROM `information_schema`.`COLUMNS`
    WHERE (`TABLE_SCHEMA` = DATABASE())
    AND (`TABLE_NAME` = '')
    AND (`COLUMN_KEY` = 'PRI');
    

    Hope it could help someone

提交回复
热议问题