0

i use MySQL. I have a table of users:

id (int primary key)
name (text)
login (text)

The table is filled in automatically. From other sources I get information from the user names in this table, take their ID. If the user is not already in the table, it should be added, and return it to ID

The scheme works out as follows:

1) get an ID on the user's behalf.

sql = "SELECT * FROM `users` WHERE `name` = 'user_name'

2) if not found, then add the user

if result is None:
    sql2 = "INSERT INTO `users` (`name`) VALUES ('user_name')

query not returns the added element ID, so you have to again make a request to know the user ID

    sql3 = "SELECT * FROM `users` WHERE `name` = 'user_name'

in the end it is necessary to make 3 query to get a user ID that is not already in the table. as it turns out a rather complicated scheme of work.

is it possible to request that, in the absence of compliance creates a record and immediately returns the data?

or may act differently? always add a new user, but to the addition did not occur in the presence of such a table. and of course to immediately return his ID?

tell me how one request to make of all this?

JayIsTooCommon
  • 1,714
  • 2
  • 20
  • 30
Massimo
  • 836
  • 3
  • 17
  • 38

0 Answers0