简体   繁体   中英

MySQL one column equal another?

How in php/mySQL can i make a column equal another column?

id is auto increment, xid should be unique. Can i make xid = id in SQL? (the reason for this is to log changes but i'll explain more if needed)

To make xid unique the best way is to copy the auto increment of id

+----+-----+----------+----------------+------+---------+
| id | xid | title    | body           | page | visible |
+----+-----+----------+----------------+------+---------+
|  1 |  1  | my title | my body        | NULL |       1 | 
|  2 |  2  | my title | my body edited |    1 |       0 | 
+----+-----+----------+----------------+------+---------+

$queryX = "INSERT INTO table (xid, title, body, page, visible) 
VALUES (, 'Plays', 'it's playing', 'book page', 1)";

I don't understand why you want to do this, but my approach would be using two queries and LAST_INSERT_ID() :

INSERT INTO table (title, body, page, visible) 
VALUES ('Plays', 'it''s playing', 'book page', 1);
UPDATE table SET xid = id WHERE id = LAST_INSERT_ID();

You may want to do this using a TRIGGER , so you don't have to do it manually.

In one query:

INSERT INTO table (xid, title, body, page, visible) VALUES 
( SELECT MAX(xid) + 1, .... )

it will put unique xid, can be done with id also, I not quite sure I undestand the need...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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