how can I do the following points in mysql (possibly using phpmyadmin):
1) Given an existing column of numeric values (colA), create a new column with text data (colB) and automatically fill this column with “typeA” if colA = 0, and “typeB” if colB > 0 ?
2) Given the following 2 tables Users and Docs:
Table Users has the columns “Username” and “UserID”,
Table Docs has columns “DocID”, “Username” and UserID” (it is redundant, I know)
The column Username in table Docs is empty and I have to fill it automatically given the UserID.
What’s the SQL code for these 2 tasks ?
thanks
For the first one, you can do:
I don’t know if it is ok how you specify in your question “and “typeB” if colB > 0“, because colB is the column where you want to write.
For your second question, you should use a view to have the username in table
Docs. To update it anyway, you can use the following:Here is how to create a view that contains the Username:
First drop the column
UsernamefromDocs.Now, when you want to select the documents, you can see the Usernames too:
And when you add a new record, you only have to specify
UserID.