In my database I have personal email field where Its containing users email. I just want to update all the user email into my test email.
Personal Email
ex.
john#gmail.com >> test1#test.com
alves#gmail.com >> test2#test.com
Note that I have more than 40,000 records so I want to update personal email field which not equal to null using sql update query
Guess your users table like this:-
| id | username | email | password |
--------------------------------------------------
| 1 | john | john#gmail.com | $jhbhbjj.. |
| 2 | alvis | alvis#gmail.com | $dcdcdcd.. |
Now we are updating every email fields to like below format
newemail = "test"+id+"#test.com".
// id is the auto increment column
We can join these strings and the column with mysql concat.
https://www.w3schools.com/sql/func_mysql_concat.asp
so we are using concat("test",id,"#test.com")
After the runing UPDATE users SET email= CONCAT("test",id,"#test.com")
you can get a table like below
| id | username | email | password |
--------------------------------------------------
| 1 | john | test1#test.com | $jhbhbjj.. |
| 2 | alvis | test2#test.com | $dcdcdcd.. |
try
UPDATE users
SET email = CONCAT("test",id,"#test.com")
this way you can use the primary key of a table instead of id in an update query.
You can use this command ;
UPDATE tblEmail SET tblEmail.column = `new value` WHERE tblEmail.column like('old value');
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. -> https://www.w3schools.com/SQL/sql_like.asp
Related
I'm trying to make a small logging table on my database.
Users
+----+------+
| id | name |
+----+------+
| 1 | FOO |
| 2 | BAR |
| 3 | LOS |
+----+------+
Log_Users
+-------------+-------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| old_id | int(11) | YES | | NULL | |
| old_name | varchar(100) | YES | | NULL | |
| new_id | int(11) | YES | | NULL | |
| new_name | varchar(100) | YES | | NULL | |
| action_type | enum('C','U','D') | YES | | NULL | |
| time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| doers | int(11) | YES | | NULL | |
+-------------+-------------------+------+-----+-------------------+-----------------------------+
I have a small application created using PHP to save user's id into session. How do i send this user's id value (on PHP's session) to a trigger of one of the tables to log their activities, like deleting another users or updating them? I've tried to use a trigger on log table to do all of the things, something like this.
CREATE TRIGGER userTrigger BEFORE INSERT ON Log_Users FOR EACH ROW
BEGIN
IF(new.action_type = 'C') THEN
INSERT INTO Users(id, name) VALUE(new.new_id, new.new_name);
ELSEIF(new.action_type = 'U') THEN
UPDATE Users SET id = new.new_id, name = new.new_name WHERE id = new.old_id;
ELSEIF(new.action_type = 'D') THEN
SET new.old_name = (SELECT name FROM Users WHERE id = new.old_id);
DELETE FROM Users WHERE id = new.old_id;
END IF;
END~
But, I'm struggling on the problem when users updating multiple records on the same column. At the end, what is and how to make an optimal activities logging using PHP and MySQL and how to do it? I have no solution for this problem for now. Thank you.
I've never done this using triggers so I can't help you with that sadly. How I usually do this:
Your users should NEVER have direct access to mysql or phpmyadmin, they should create, edit, delete and anything else using a PHP script you provide. This way you have total control over what your users can and can't do, and you narrow a lot the posible actions performed, so creating logs of them is much easier. For example:
You have a php scrip that users use to do some stuff and insert a new row, right after that you do a insert on the log table recording this last action.
I've created a database using MySQL with PHP as it's front end. And for end users I've created a username table and data excess level table, I've also created a table in which I've linked usernames with the products against which I wanted users to see their respective products only. It was executed flawlessly, but it only pick the first product of each user and leave the others.
I am thinking to create a separate table with the name of group in which I can link it with username and excess level table. But I am unable to think of a way to do this.
Any better idea to do this without any difficulties?
Table structure:
--------------------------------------------
| username | product_user | excesslevel |
--------------------------------------------
| usernameid | pu_id | excessid |
| username | productname | excesslevel |
| password | username | username |
|-------------|--------------|-------------|
------------------------------
| username | product_user |
------------------------------
| userId | pu_id |
| username | productname |
| password | userId |
|-------------|--------------|
If I userstand this correctly... A user can only access their own products.
If that is true, query your database with:
SELECT * FROM product_user WHERE userId = 1
This would give you every products that belong to user 1. A third database would be required to assign a same product to different users.
Also, don't use the username in another table. If the username change, you'll need to update many rows from different tables. Assign it's ID instead.
I have the following Mysql-table:
+-------+--------+------------+----+-------+
| Name | Number | Department | id | JobID |
+-------+--------+------------+----+-------+
| Sven | 2204 | Marketing | 10 | 111 |
| Peter | 2304 | IT | 20 | 222 |
| Bjorn | 4409 | IT | 30 | 333 |
+-------+--------+------------+----+-------+
I get the three columns: Name, Number, Department from a system where I don't have the id and need to perform something in my php script.
Now I would like to performa an insert if there is a new record. If there is an existing record I would like to perform an update, if something changed like Name, Number or Department.
For example, if Number changes it should compare Name and Department and then change number. If Departmend changes it should compare Name and Number and then change Department and so on.
The problem is, that I can not use insert...on duplicate key, because I don't get the primary key.
If I use Replace Into it deletes me also the entry for JobID. Is there a solution how to perform a sql that it will insert and also update if there is now entry? Or something that can do the trick?
Thanks for your help!
I'm trying to find out the best way store array of items with details in SQL table.
I have a user account database. The user have multiple input fields to enter multiple details like:
___ : ______ +(get more multiple field field)
User can input any details like
Output1 : Output2
Mobile : 2455...
email : sdf
city : dfs
Other : sf
On an average a user will use around 20 options
Since the fields (mobile, email etc.) are not known to me, I have to store Output1 field with the answer field (output2).
I will be having a very huge user base, so I think it's not better to create separate tables for each user.
Is there any way to store and get the details in limited or single column.
Since both the attribute name and value comes from users, a typical 3-table model of saving many-to-many relationship is a bit of overkill.
I would just kept users and their attributes in two separate tables:
+---------+-----------+--------------+
| user_id | user_name | user_email |
+---------+-----------+--------------+
| 1001 | John | john#doe.com |
+---------+-----------+--------------+
| 1002 | Tim | tim#doe.com |
+---------+-----------+--------------+
+----------+-----------+--------------+--------------+
| field_id | user_id | field_name | field_value |
+----------+-----------+--------------+--------------+
| 1 | 1001 | Option1 | Option2 |
+----------+-----------+--------------+--------------+
| 2 | 1001 | Mobile | 2345656565 |
+----------+-----------+--------------+--------------+
| 3 | 1001 | city | dfs |
+----------+-----------+--------------+--------------+
| 4 | 1002 | Other | something |
+----------+-----------+--------------+--------------+
Possibly with some additional columns for sorting, tagging, etc.
I would use 3 tables.
Table 1 - user. PK is UserId. Other fields are name, rank, serial number, etc
Table 2 - Attribute - Primary key is AttributeId. Other Field is attribute name. Examples of attribute names are emailAddress, cellphoneNumber, cityName.
Table3 - UserAttribute. Primary Key is UserId and AttributeId. Other field is Attribute value.
I have a PHP script where you can (as admin) select how many input-fields there will be in a question form. Some of the fields are non optional, but som are (as many as you like).
The table in MySQL for collecting the answers looks like this:
id | userid | fname | ename | seat | optional
If the admin want it to be two optional input-fields then the result of one filled form would take three tows in the table:
| 5 | 3 | Peter | Pan | 4 | |
| | 3 | | | | opt.value1 |
| | 3 | | | | opt.value2 |
Is this really the best way to store this in? How would you solve it?
And also, how can I make shure that the userid is unique for the user? I can't use the auto-increment key value thing in MySQL because the same value is on three rows...
The way i learned it you have to use multiple tables. Like this:
Table1:
id | userid | fname | ename | seat
Table2:
userid | optional
Table2.userid is a reference to Table1.userid
Then the fields that has to be filed can be put into the first table and all the optional in the second.
If i follow your example your database should look like this:
Table1:
id | userid | fname | ename | seat
5 | 3 | Peter | Pan | 4
Table2:
userid | optional
3 | opt.value1
3 | opt.value2
By the way, why do you have both id and userid in Table1?
Best practice would be to store "id" and "optional" values in a separate table.
Then pull the information you want from it for each "id".