Bulk update in MYSQL - mysql

I have a table containing data about some users.
Many of them use our in house email system at OLDHOST.com. We have updated to a newer system at NEWHOST.com. All the users usernames are the same, so if you had dave#OLDHOST.com, you are now dave#NEWHOST.com
Is there a better way to change all the email fields in the users table without selecting all the rows in say PHP, then checking if the email has OLDHOST in it, then replacing the string to NEWHOST?
Is there a baked in awesome SQL statement to help with this?
Example of some of the table (simplified)
id | firstname | surname | email
------------------------------------------------
1 | dave | smith | a21dsmith#OLDHOST.com
2 | barry | jones | a21bjones#OLDHOST.com
etc.
All that needs to be changed is any emails that contain OLDHOST (Not all do) to NEWHOST.

You'll need to replace the relevant part of each string within an update statement, grabbing the hostname substring after the # with a REPLACE, and replacing it.
UPDATE table SET email=REPLACE(email,'OLDHOST.com', 'newhost.com');
Note: REPLACE() is case-sensitive, so you can use LOWER(email) inside the REPLACE function if you need to catch all case possibilities, as below:
UPDATE table SET email=REPLACE(LOWER(email),'oldhost.com', 'newhost.com');
This will also convert all your email addresses to lowercase, so be aware of that :)

Related

How to extract relational data from a flat table using SQL?

I have a single flat table containing a list of people which records their participation in different groups and their activities over time. The table contains following columns:
- name (first/last)
- e-mail
- secondary e-mail
- group
- event date
+ some other data in a series of columns, relevant to a specific event (meeting, workshop).
I want to extract distinct people from that into a separate table, so that further down the road it could be used for their profiles giving them a list of what they attended and relevant info. In other words, I would like to have a list of people (profiles) and then link that to a list of groups they are in and then a list of events per group they participated in.
Obviously, same people appear a number of times:
| Full name | email | secondary email | group | date |
| John Smith | jsmith#someplace.com | | AcOP | 2010-02-12 |
| John Smith | jsmith#gmail.com | jsmith#somplace.com | AcOP | 2010-03-14 |
| John Smith | jsmith#gmail.com | | CbDP | 2010-03-18 |
| John Smith | jsmith#someplace.com | | BDz | 2010-04-02 |
Of course, I would like to roll it into one record for John Smith with both e-mails in the resulting People table. I can't rule out that there might be more records for same person with other e-mails than those two - I can live with that. To make it more complex ideally I would like to derive a list of groups, creating a Groups table (possibly with further details on the groups) and then a list of meetings/activities for each group. By linking that I would then have clean relational model.
Now, the question: is there a way to perform such a transformation of data in SQL? Or do I need to write a procedure (program) that would traverse the database and do it?
The database is in MySQL, though I can also use MS Access (it was given to me in that format).
There is no tool that does this automatically. You will have to write a couple queries (unless you want to write a DTS package or something proprietary). Here's a typical approach:
Write two select statements for the two tables you wish to create-- one for users and one for groups. You may need to use DISTINCT or GROUP BY to ensure you only get one row when the source table contains duplicates.
Run the two select statements and inspect them for problems. For example, it's possible some users show up with two different email addresses, or some users have the same name and were combined incorrectly. These will need to be cleaned up in order to proceed. There is great way to do this-- it's more or less a manual process requiring expert knowledge of the data.
Write CREATE TABLE scripts based on the two SELECT statements so that you can store the results somewhere.
Use INSERT FROM or SELECT INTO to populate the tables from your two SELECT statements.

how to check for multiple groups of values exists in same table from database?

This is my sqlfiddle
http://sqlfiddle.com/#!9/e443688/2
If I have a list of values:
a#test.com
b#test.com
c#test.com
And my table has a column called email I can run a query like
select email from datatable where email in ('a#test.com', 'b#test.com', 'c#test.com')
to find whether they exist.
However, if my list of values consists of at least 2 columns:
a#test.com, manager
b#test.com, editor
c#test.com, editor
And my table has two columns email and job_title, how do I run a query to check if there are rows that match the values I am searching for?
I don't want to know how many there are. I do want to know which value group exists.
Ideally if my query can return back something like this
email | job_title | exists
------|-----------|-------
a#test.com | manager | 1
b#test.com | editor | 0
c#test.com | editor | 1
Or just
email | job_title
------|-----------
a#test.com | manager
c#test.com | editor
That's good enough
What 'marekful' suggested is a pretty good option. If you are looking for an alternative way, here's another option:
select email
from datatable
where (email = 'a#test.com' and job_title = 'manager')
or (email = 'b#test.com' and job_title = 'editor')
or (email = 'c#test.com' and job_title = 'editor');
I used #marekful comment and concatenate the two values.
Also #Strawberry's suggestion works as well.
Here's the fiddle. http://sqlfiddle.com/#!9/e443688/3
And the query was
select id from emails where (email,job_title) in(('a#test.com','manager'),('b#test.com','editor'),('c#test.com','editor'))

Find existence of a record in MySQL table with input data as a list

I have a list of ids in text format as a comma separated value like so
("12345", "12346", "12347", etc, etc)
I would like to find their existence or non existence from a table say devices table which has a column called device ids (not primary key)
Ideally i would like to get a list which says if each item exists or not.
So far I have tried to get the query of those that exist and I have to manually find the non existing ones.
Is there a for loop I have to run on stored procedures or something like that. Please help.
Table structure
<pre>
| id | device_id | device_name |
+------+-----------------+---------------+
| 71 | 352701060409650 | 57X |
| 13 | 352701060409700 | 582 |
</pre>
You need to create a query with left join to the same table with 'IFNULL' condition. There already has been a post for this topic. Please check this out here.

Advance search on database column

I have a Table say tblName having fields and values
| ID | Name |
| 1 | Technical University |
| 2 | XYZ Lab Ltd. |
Now I have to match Name column with the following input values :
1- tech univ
2- tech universities
3- xyz Labs
I am not able to write query to get result from the table.
If I match Name column with the input value tech univ or tech universities then query should response result set Technical University from the tblName.
Please suggest.
Thanks in advanced.
Well you can do one thing map name to different names such as
ID I_ID NAME
1 1 tech university
2 1 tech uni
3 1 tech univs
So this table will be link for your search queries I_ID is a foreign key which refers ID in primary table. I guess this could solve your issue and you can define several names for a single name.
Moreover use regular expression for more efficiency and you have to develop a custom function for that though the genral usage of REGEXP is as follows
SELECT id, name FROM tblName WHERE name REGEXP 'tech'
Please refer http://www.tutorialspoint.com/mysql/mysql-regexps.htm for more on REGEXP
For simple cases when you need to match begining of string you can use
select * from table where field LIKE 'Hello %'
The % sign is a wildcard.
But for the example you need I would rekomend to look on some full text indexing solutions that you put around mysql.
In MS SQL we use a Levenstein Distance function and you could build your own and extend mysql with it.
Here is some examples but I do not know if it will work
https://github.com/jmcejuela/Levenshtein-MySQL-UDF
https://github.com/jmcejuela/Levenshtein-MySQL-UDF/blob/master/levenshtein.c
Found one that should be pure SQL
http://openquery.com.au/blog/levenshtein-mysql-stored-function
test this it may help
SELECT * FROM tblNAME WHERE Name LIKE "%name%";

Database issue - how do I set up user accounts/pswds so they can ONLY add/change THEIR data?

Okay... I am working to create a mobile app that allows two groups of users to do two different things.
Essentially, the goal of the project is this:
Group A users: create account/pswd and can enter THEIR data into the database and/or change THEIR existing data (but ONLY their data)
Group B users: can SEARCH the database for information that is inserted by Group A. Down the track I'd like to set it up so that they can create an user account so they can also SAVE key information to THEIR account for faster recall (so they don't have to look up the info they search for regularly) -- but that is down the track.
I have a relational database set up using the mySQL that is available with my web-hosting account (it seemed to be the easiest way to go).
I'm just trying to work out how to handle the user account creation/authentication bit, because each group should ONLY be able to CHANGE/INSERT data to their own account, but can search for information submitted by anyone else.
Thanks in advance.
Use mysql facilites to manage permissions: roles, users and privileges.
Navigate through mysql official documentation (i.e. http://dev.mysql.com/doc/workbench/en/wb-adding-roles.html).
You can create two roles: groupA that can INSERT/SELECT/UPDATE one set of tables, groupB that can do the same but in another set of tables.
You can assign INSERT privilege in just the table you want, but SELECT privileges on all the tables.
Hope this info brings you some light...
Firstly this sounds like a huge project, I am sure there are frameworks out there that can do this for you. However, if you are trying to do this on your own continue reading.
This can be done several ways. I will try to be as detailed as possible. This requires SQL as well as application development/Software engineering knowledge.
Step 1: Setup your database
You will need the following tables: All ids are primary keys auto incremented, the other fields can be varchar, except fields that have date in their name
sessions [id, uid, random_token, datecreated]
resourcescope [rid, name]
user [uid, first, last, email, username, salted_pwd]
user_type [id, name, description]
user_resourcescope [id, uid, rid] //lookup table between userid and resourcescope
I prefer using Java or python because you can use dependency injection or decorators. As a result, you don't have to write a lot of code when checking if a user has access.
Putting it all into practice.
1. When a user signs up, you save them into a user database. Depending on the user type, you give them different permissions. Next, you save the user permissions inside the user_resourcescope table.
You should now have the following.
User Table
UID | first | last | email | username | salted_pwd | usertype
1 | james | iri | example#isp.com | jiri1928 | klasdjf8$kljs | 1
UserType table
usetype_id | Name
1 | Basic users
2 | Searcher
ResourceScope Table
rid | Name
1 | FindContent
2 | CreateContent
3 | DeleteContent
User_Resourcescope
id | uid | rid
1 | 1 | 1
2 | 1 | 3
Session
id | uid | random_token | datecreated
1 | 1 | ldkjfald882u3u | 1391274870322
Each resource represents a request within the system. For example,
http://api.myapi.com/content/add - This would be associated with the ResourceScope CreateContent
http://api.myapi.com/content/delete- This would be associated with the ResourceScope CreateDelete
http://api.myapi.com/content/search - This would be associated with the ResourceScope SearchContent
When someone tries to create content, you check if their cred are correct by validating their session information and you check to see if they have the correct permission by checking the User_Resourcescope table.
To prevent users from deleting content that is not theirs. Inside the content table you can add a creator field and put the user id associated with the content. And if someone try to delete content you can check their user id against the creator field.