On 01/02/2010, at 7:33 PM, 曹凯 wrote:
Hi all,
if we just know the table name but don't know the name of primary
key, is there any variables or constants could instead of the PK?
for example:
there is a table "game_log", and now I have the last inserted_id
but don't know what its primary_id is,
how can I "SELECT * FROM game_log WHERE this_table's_PK =
last_inserted_id"?
You can get the column name from the information schema, however that
can't be used directly in another query in the way you've done in
your example. E.g.
game> SELECT COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = 'game' AND TABLE_NAME = 'game_log' AND
CONSTRAINT_NAME = 'PRIMARY';
+-------------+
| COLUMN_NAME |
+-------------+
| GameLogID |
+-------------+
1 row in set (0.00 sec)
where it is assumed the database name is "game".
Hope that helps.
Jesper