I have a MySQL table with two copies (one in local server, one in web host) and I want to update some fields in my local server, than update only those values in my web host.
So say my table is like
Table in localhost, initially :
id username money
---- ---------- -------
1 user_01 0
2 user2 0
Table in web host, initially :
id username money
---- ---------- -------
1 user_01 1000
2 user2 2000
Than I want to change the username user_01 to user1, and update the web database, but not change the money field
Table in localhost, after change :
id username money
---- ---------- -------
1 user1 0
2 user2 0
&
query -> ????
Table I want in web server, after change :
id username money
---- ---------- -------
1 user1 1000
2 user2 2000
I tried to delete the table and create it from scratch but then the money value becomes 0, which I want to avoid.
So what query / type of export should I use (in phpMyAdmin or whatever) to update only certain fields?
Thanks !
Isn't it like this?
UPDATE localhost
SET username = 'user'
WHERE username = 'user_01'
OR
UPDATE localhost
SET username = REPLACE(username, '_0', '')
WHERE username LIKE '%\_0%'
expr LIKE pat [ESCAPE 'escape_char']
Related
My Use Case :
I am trying to create a GUI and implement it with MYSQL Database. The problem I am facing is the scenario when I have to update a certain entry in the Database.
I know that we can update an Entry in MYSQL database using :
ALTER TABLE <TABLENAME> SET <PARAMETERS=NEW VALUES> WHERE <CONDITION> ;
For eg : If I want to change the name of the guy who id is 2 , I have to write :
ALTER TABLE StudentInfo SET Name='ABC' WHERE id=2 ;
But the problem is , in a GUI based environment , a user can choose to update any particular value wihtout having a constant condition like id in the previous example.
In the UI , the user can opt to select anything from the parameters and modify it and then click the update button.
Now How will I figure out what <CONDITION> to put in the MYSQL query when I need to update the database ?
Any help would be greatly appreciated !
you update a by using the UPDATE command not ALTER, which will change table. Your gui already knows ho tow identify the row in your case for example by the column name
UPDATE StudentInfo SET Name='ABC' WHERE Name='QUERTY';
SEE example
CREATE TABLE StudentInfo(
Name VARCHAR(20),
class int,
section VARCHAR(2),
roll_no int
);
INSERT INTO StudentInfo VALUES ('abc',12,'A',18), ('xyz',12,'A',17),('QUERTY',12,'A',16)
UPDATE StudentInfo SET Name='ABC',class = 15,section = 'B',roll_no= 99 WHERE Name='QUERTY';
SELECT * FROM StudentInfo
Name | class | section | roll_no
:--- | ----: | :------ | ------:
abc | 12 | A | 18
xyz | 12 | A | 17
ABC | 15 | B | 99
db<>fiddle here
The main problem is to identify the correct row, so you should have a field that is unique.
Like an id auto_increment, that is invisible for the user, but you can identify every row and use this id to update the row.
UPDATE StudentInfo SET Name='ABC' WHERE id = 3;
So that if you have two rows with John Smith you still could update the right one
I have a use case for range based shard in mysql i.e
---------------------
shard | upperbound
---------------------
1 | 500
2 | 1000
3 | 1500
Meaning Shard1 has 1- 500 users and shard2 has 501-1000 users and so on. So that latter point on user base grow, will keep on adding new shards.
The user table looks like below:
-----------------------------
id | name | email | contact
-----------------------------
1 |Test |t#t.com|xxxxx
501 |Test1 |t#t.com|xxxxx
101 |Test2 |t#t.com|xxxxx
1001 |Test3 |t#t.com|xxxxx
So user with name Test resides in shard1, user with name test2 resides in shard2 and user with name Test3 resides in shard3 and so on.
When a new user registration happens, Based on the open shards lets say (Shard4 and Shard5 is open for registration) then it first does a select on the user table with range to get the next max possible Id and increment by 1 and stores it. Problem with this approach is when two different people tries to register and from code select returns same id for the two different people/thread as both the user registration can't be in same transaction.
Is there any other better way for choosing the next id for a shard using range ?
select max(id) from t for update
Should hold the lock on the max id till the transaction completes.
I am new here and with PHP/MySQL programming. I need do the next consult in my database.
I have two tables:
-The first of relations between users where friends have the value "2"
The columns is like:
id | Username1 | username2 | relation
-The seconds is for messages post by users
The columns is like:
id | username | message
The id is the "primary key" with auto increment property. (in both)
I want to show all message of friends of one user by order of post (Most recently first)
So I need select the max value id where the user1 and user2 is friends, but I need select the max value of "messages table" and check if they are friends in the "relations table"
I know what the next is not the syntax but I need something like this:
SELECT message
FROM message_table
WHERE MAX(id) AND ( FROM relations
WHERE ( ( user1="randomuser"
OR user2="randomuser")
AND relation="2")
)
ORDER BY id DESC
I know what that is wrong... but I need that two filters to show all friend message in posting order.. So how can I show register from one table with a one condition in that table and the second condition in other table.
Edit: Example:
id | Username1 | username2 | relation
1 Randomuser1 Randomuser2 2
2 Randomuser1 Randomuser3 0
3 Randomuser1 Randomuser5 2
id | Username | message
1 Randomuser1 Hi this is a random message?
2 Randomuser2 I don"t like your message
3 Randomuser5 Sure, is a bad message
So I want to show all message of friends of randomuser1 by order of posting
First the user"s friends of Randomuser1 is
Randomuser2 and Randomuser5 (The "2" Mean they are friends)
so i want to show all messages of their friends by order of posting (By id order of second table (message_table))
So i need a output like:
id | Username | message
3 Randomuser5 Sure, is a bad message
2 Randomuser2 I don"t like your message
That is the output what I need when I choose the messages of friends of user Randomuser1
I have a Users table that belongs to a Role, and has one Server or no one (depends on role), but should to have a Server table with user_id field or should I put all Server info in Users table and when the role hasn't Servers, the fields will be null?
I just think that if a User have one server (or not), this shouldn't be a new row in Servers table, maybe if the user_id field be unique, then it will be correct, I don't know.. I'm confused.
Just explain to me which is the best way to build this thing.
-- edit
This is my tables actually
Roles
id (PK) | name
1 | Administrator
Users
id (PK) | role_id | name
1 | 1 | Juliano
Servers
id (PK) | user_id (UNIQUE) | name
1 | 1 | Test
I don't know.. in servers, user_id should be UNIQUE or PK?
ONE TO ONE Relation , Then put the server in the user table.
Users Table:
ID(Pk)
RoleID
Name
ServerID
I have a scenario where I need to insert the data into table temporarily and later on approval or confirmation, make it permanent. The data will be inserted by a user and approval or denial needs to be done by Super User.
What I think of now is to have two different but identical tables (temporary and main) and the user will insert the data into temp table. After confirmation of Super User, the data will be moved to main table. But the problem comes when a database contains very large number of tables then this process will become more complex.
EDIT : This implies to CREATE EDIT & DELETE commands.
Is there any simpler or better approach of doing this?
Please suggest.
Using a version table (related to comment):
The idea here is to have a version table; when your user changes a piece of information the new version is stored in this table along with the related ID.
Then all you need to do is join on the PersonID and select the most recent accepted version.
This means the user can make as many updates as they want but they won't show until the super user accepts them, it also means the data is never destroyed (stored in the version table) and they don't need to implement rollback as it's already there!
See: http://sqlfiddle.com/#!3/cc77f/4
People Table:
ID | Age Etc... (Info That Doesn't Change)
-----------------------
1 | 12
2 | 16
3 | 11
People Version Table:
VersionID | PersonID | Name | Approved
-----------------------
1 | 1 | Stevz | FALSE
2 | 1 | Steve | TRUE
3 | 2 | James | TRUE
4 | 3 | Jghn | FALSE
5 | 3 | John | TRUE
Example table SQL
CREATE TABLE People
(
id int identity primary key,
age int
);
CREATE TABLE PeopleVersion
(
versionId int identity primary key,
peopleId int,
name varchar(30),
approved varchar(30)
);
Example Query
SELECT * FROM People p
INNER JOIN PeopleVersion v ON p.id = v.peopleID
WHERE v.approved = 'TRUE'
ORDER BY versionId DESC
A further insight:
You could even have three states of Approved; null meaning no admin has chosen yet, TRUE meaning it was accepted and FALSE meaning it was rejected
You could show the user the most recent from null and true, show the admin all three and show the other users of the site only versions that were true
Old Comments
Could you just add a field called approved to the table and then hide anything without the approval flag set to TRUE?
It could default to FALSE and only the super user would be able to see items with the flag set to FALSE
E.g.
Name | Age | Approved
-----------------------
Steve | 12 | FALSE
James | 16 | TRUE
John | 11 | FALSE
The user would only see James, but the SuperUser would see all three listed
Alternatively using your temporary and main tables is the other way of looking at this problem, though this may lead to problems as everything get's larger
The easiest approach is a flag within the table marking an entry either approved or not-yet approved.
Then just change the retrieving logic to only show entries where that flag is set to approved.