Rename/change entries depending on other MySQL columns - mysql

I have a database with a lot of entries for images that I want to rename. Let's say all the images have the name Something_X where X is a number, this is in a column called "name" of my database. There are 2 columns in the database that have the original filesize called "original_width" and "original_height". I want to change all the entries in "name" that it still has the original but it adds the original size to it so that "Something_X" becomes "Something_X_widthxheight", is this possible and how would I do this with PHP my admin in a simple way with preferably a SQL code.

UPDATE <yourTableName>
SET name = CONCAT(name,'_',original_width,'x',original_height);
To update only rows that haven't been updated, you can use the below (sqlFiddle)
UPDATE yourTable
SET name = CONCAT(name,'_',original_width,'x',original_height)
WHERE name NOT LIKE CONCAT('%\_',original_width,'x',original_height);

Related

How can i detect if a new row is already in the database, a data change or conflict case?

The Data for my MySQL Database is imported from excel files via LOAD DATA IN FILE every night. Then they are temorarily stored in the table projekt2.
For every row in projekt2 i want to check if there is already one row in projekt1 that matches exactly (all columns), then it should not be imported.
If one column is different, the row should be identified as correction and the old row should be altered.
If two, three, for or five columns are different it should be identified as a conflict and the system should send an email and ask what to do.
If more than five columns are different it should be identified as a new row and be added to the table.
My Mysql-Version: 10.1.37-MariaDB-0+deb9u1
I started to check every Possibility like All are equal, then only the first is different, but all the rest is equal, then only the second etc.
But this will be a lot of queries in the end and I hope, there is a more efficient way to solve this in counting the duplicate columns.
UPDATE
SELECT * FROM projekt2
WHERE EXISTS (
SELECT id FROM projekt1
WHERE projekt1.datum = projekt2.datum
AND projekt1.begleitschein = projekt2.begleitschein
AND projekt1.menge = projekt2.menge
AND projekt1.abfallschluessel = projekt2.abfallschluessel
AND projekt1.entsorgungsnachweis = projekt2.entsorgungsnachweis
AND projekt1.befoerderer = projekt2.befoerderer
AND projekt1.erzeuger = projekt2.erzeuger
AND projekt1.waegennummer = projekt2.waegennummer
AND projekt1.waage = projekt2.waage
AND projekt1.kfz = projekt2.kfz
AND projekt1.charge = projekt2.charge
AND projekt1.art = projekt2.art
),

How to To Write whole .txt file to a Column field through MySQL

I Want To Write whole .txt file to a Column field through MySQL.
I have a table called DBInfo with bunch of records, I just inserted a new column called ExtraInfo. Now I have 100's of text files in my local drive. I just want to write each text file to newly added column.
For Example : for primary key ID = "1" , I want to write the file C:\client\1.text to the newly added column ExtraInfo , similarly for ID = "2" I need to write C:\client\2.text in respective field,
and so on...
Thanks In advance !
If you just want to put the filenames into the column, you can do it with a simple UPDATE statement that calculates the filename from the ID.
UPDATE DBInfo
SET ExtraInfo = CONCAT('C:\\client\\', ID, '.text')
If you want to put the contents of each file into the column, there's no way to do that just in SQL. You could do it with a PHP script that reads each file into a variable and then uses the variable in an UPDATE statement.

UPDATING partial string from a field mysql

I have a table with columns like:
emails, country_code etc
Some of the rows contain emails like:
XXXXX#googlemail.com
I want to change about 10,000 records like this in a way that will affect only the "googlemail.com" part of the value and change all of them to "gmail.com".
UPDATE exmple_table SET emails = REPLACE(emails, '%googlemail.com','%gmail.com');
I tried to find and replace but that making me have to type all 10,000 addresses in the query, any solutions?
You can use 'like' operator to filter out the records which contain 'googlemail' and then perform the string replace on them, as shown below:
update table
set SET emails = REPLACE(emails, 'googlemail.com','gmail.com')
where emails like '%googlemail.com%'

How to get filename from file, look up that value in SQL table column, then return value from other column using SSIS?

Using SSIS (2008) and T-SQL: How can you get the filename from a file and look up that value in a SQL Table column and then return a value from another column?
I have a folder which has .jpg image files of products. All the filesnames are in the format eancode.jpg, for example 1234567891023.jpg. Each filename is unique: one eancode per image file for every product.
The productID (primary key, varchar) and the eancode (varchar) are stored in the same SQL table (without the .jpg extensions of course).
What I would like to do is rename the file from eancode.jpg to productID.jpg.
This is the process I had in mind, for example:
Files in FolderA:
ean1.jpg
ean2.jpg
ean3.jpg
Steps/Tasks:
get filename ean1 from ean1.jpg
look up ean1 in Table column EANcode
return corresponding productid: select productID from Table where EANcode = 'ean1'
store returned value from step 3 into package variable (?)
use stored value of step 4 to rename filename
do step 1 - 5 in a foreach loop for all images; If no match can be found it should do nothing with the files.
The main focus of my question is on step 2 - 4.
Thanks!
Your suggested solution would work perfectly. Set the result set (middle of the general tab) of the execute sql task to single value and then map the zero based result value to your package variable from within the result set tab

MySQL modify values of one column based on contents of another

Basically I want to query a database and modify the values of one column based on the contents of another.
Here's my idea of how it would work:
IF Column 'Town' IS NOT NULL then Column 'Sign-up type' = 1 else = 0
The logic is, i've added a new column into the DB that will store whether a quick or full sign up has been made.
Quick = 0, Full = 1. Default is 0 = Quick.
I've managed to implement the change on the two registration forms that feed the DB, but I need to append the historical data to backwards fill the data.
Because the quick sign up only collects name, and email, those entries do not contain data in the 'Town' field which is a required field in a full sign up.
So i'm using that as a reference point to select all the entries that DO have (NOT NULL?) data in order to enter '1' (representing 'Full') into 'Signup Type' column.
I hope I'm making sense! I only have a basic understanding of MySQL but I'm willing to learn, it's sometimes hard trying to explain what I want to do when I'm unclear of the correct jargon!!
UPDATE yourTable SET signupType = IF(Town IS NULL, 1, 0);
Note this will update all data, you may want to limit this to historical data (by the sounds of things this should be fine however).