R
R
Rustam Zainullin2018-09-18 13:50:17
SQL
Rustam Zainullin, 2018-09-18 13:50:17

How to compose correct update sql (with nested query) query in db?

I have a query like:

UPDATE
             requests
SET
             requests.title = "договор для АО",
             requests.responsible = (SELECT requests.responsible, users.id FROM requests, users WHERE "переменная с текстом" = users.username),
       requests.datecreate = "2018-09-18",
       requests.percent = "0",
       comment = "коммент",
             status = (SELECT requests.status, status.id FROM requests, status WHERE "переменная с текстом" = status.status)

WHERE
             requests.id = 4

Question: how, for example, to return the id from the users table when there is a match in the users.username column with a text variable?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan., 2018-09-18
@tavrikanec

You didn't specify how the requests and users tables, as well as the requests and status tables, are related.
Both subqueries in this case must return one row and one field for each requests entry, otherwise there will be an error.
If there is no connection, you can try this:

UPDATE requests
   SET requests.title       = "договор для АО",
       requests.responsible = ISNULL((SELECT id 
                                        FROM users 
                                       WHERE username = "переменная с текстом"), requests.responsible),
       requests.datecreate  = "2018-09-18",
       requests.percent     = "0",
       comment              = "коммент",
       status               = ISNULL((SELECT id 
                                        FROM status 
                                       WHERE status = "переменная с текстом"), requests.status)
 FROM requests
WHERE requests.id = 4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question