delete duplicate with a specific criteria - duplicates

I have a table and remove all duplicates according to "serviceWoNumber" but code should decide which duplicates shoould remain by considering "type"
First table:
serialNo serviceWoNumber type
900017672280000 1700028713 CD
731127155164500 1600712441 FD
731127155164500 1600712441 CD
900017672280000 1700028713 CD
900017672280000 1700028713 CD
359864066017456 325088621 FD
359864066017456 325088621 CD
Table I need:
serialNo serviceWoNumber type
900017672280000 1700028713 CD
731127155164500 1600712441 FD
359864066017456 325088621 FD
I would be very appreciated for any help
Thank you

Related

how can i tak all record from parent table and specific record from child table based on some values in one view

I need takes all record from rooms_tbl table and take some record from aqed_tbl table based on (state) values
state values : rested,busy,finished ,deleted
where state=rested or state=busy
how can i do this in one view ???
I guess that the answer to your question is the following SQL:
select rm.*, ag.*
from rooms_tbl rm
inner join aged_tbl ag on rm.id = ag.room_id
where ag.state in ('rested', 'busy')
If we need all rooms even if they don't have an aged_tbl entry, then this statement should have an outer join:
select rm.*, ag.*
from rooms_tbl rm
left join aged_tbl ag on rm.id = ag.room_id
where ag.state in ('rested', 'busy')

Update multiple rows with corresponding values from another table MySQL

Using MySQL, I'm working on a script that will import data from a CSV file. I've gotten to the point where the script is finished for importing data for a single user, however that I want to extend to all users now. A statement I currently have is the following:
UPDATE werte
SET werte=(SELECT Date_Enrollment
FROM THKON01.data
WHERE auto_patient_id = 1020)
WHERE folder_id = 1525
AND number=4;
Now what I want is to use the enrollment dates from all users (so I would omit the "WHERE auto_patient_id ..." statement) and insert them into all corresponding rows. Here lies the problem. I tried for two users at once with the statement
UPDATE werte
SET werte=(SELECT Date_Enrollment
FROM THKON01.data
WHERE auto_patient_id = 1020
OR auto_patient_id = 1051)
WHERE folder_id between 1524 AND 1525
AND number=4;
However this gave me an error that said "Query returned multiple rows", referring to the inner query of SELECT Date_Enrollment.
Note that the auto_patient_id's are not sequentially numbered, so I can't use a "between" there.
EDIT: For clarification
I have two tables. One, werte, is where I want the values to be stored to. THKON01.data is the table I want to read the values from. In case of this example, I want the Date_Enrollment values to be written into the werte table. Let's say I have 3 users I want to do this for, then the structure for THKON01.data looks like this:
auto_patient_id Date_Enrollment
1020 01.01.1911
1050 02.01.1912
1073 03.01.1913
... ...
Now I want to insert this into the werte table which looks like this:
folder_id werte
1525 <empty>
1526 <empty>
1527 <empty>
... ...
I want them to be inserted so that the first value of THKON01.data (01.01.1911) gets copied to the first value in werte (field of folder_id 1525), the second (02.01.1912) gets to the second (folder_id 1526), and so forth. Folder_id is sequentially numbered, auto_patient_id is not. I hope that clarifies this a little.
If you have some links between fields auto_patient_id and folder_id you can try something like this
UPDATE werte
SET werte=(SELECT Date_Enrollment
FROM THKON01.data
WHERE (your_link))
WHERE number=4;
here your_link can be of THKON01.data.auto_patient_id = werte.somefield or THKON01.data.auto_patient_id = somefunction(werte.folder_id)
It will select only one record at a time and update all your records that fall under outer where condition.
update
if you want to use some bash script you can use smth like this
$folder_id = 1 # or some other start number
mysql -e "SELECT Date_Enrollment FROM THKON01.data" | while read Date_Enrollment; do
mysql -e "update werte set werte = $Date_Enrollment where folder_id = $folder_id"
$folder_id = $folder_id + 1
done
You said that your folder id are in order, so we can just add 1 each time instead of fetching them from result.
I'm absolutely not proficient with bash scripting so this script could be not working, but I hope that the idea is clear.

mysql update adding row instead of updating

i have a shell script that issues a sql query that will replace a filename in the database with another filename. it also will replace the full path by concatenating the name of the file with the name of the directory stored in a different table. it works for me but when another user tries it on another database (identical structure but different data) it will add a new record instead of updating it. i've searched but i cant figure out why it works for me and not them. all i can think of is maybe it is a permission issue. here is the query:
UPDATE My.files
SET strFilename="'$NFescaped'"
WHERE strFilename="'$DFescaped'";
UPDATE My.episode
SET c18 =
(SELECT concat (
(SELECT strPath
FROM My.path
WHERE path.idpath=
(SELECT idpath
FROM My.files
WHERE strFilename="'$NFescaped'")), "'$NFescaped'"))
WHERE c18 =
(SELECT concat (
(SELECT strPath
FROM .path
WHERE path.idpath=
(SELECT idpath
FROM My.files
WHERE strFilename="'$NFescaped'")), "'$DFescaped'"));

MYSQL - Updating a SET Value

Say for example you had a CD Database, where you wanted to increase the CD prices of all CD's with the genre 'Pop' by 10%. However the type Genre is a SET. Where it can be in multiple genre's, such as RnB and Rock.
My code is as follows:
UPDATE CD
set price = price * 1.1
WHERE genre = 'Pop';
However my code is only updating rows where the Genre is ONLY pop. If the Genre is 'Rock,Pop,RnB', it is not updated. What am i doing wrong?
You need to use LIKE (Documentation):
... WHERE genre LIKE '%Pop%'
This will match if your genre is "Rock,Pop,RnB."
You should be aware that you are using a non-normalized structure. A better design would be to have a genre reference table:
CD_Genre (CD, Genre)
use like keyword
UPDATE CD set price = price * 1.1 WHERE genre like '%Pop%';
The best way to access SET values in queries is FIND_IN_SET:
UPDATE CD
set price = price * 1.1
where FIND_IN_SET('Pop', `genre`)
More information about FIND_IN_SET can be found here: http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set

magento product visibility

Is there a way by mysql to set ALL product visibility to catalog, search?
open up the eav_attribute table and find the row where attribute_code = visibility. Take note of the attribute_id, most likely it will be 85. Also take note that backend_type = int. This tells you that the attribute is stored in catalog_product_entity_int. So, now you can run:
update `catalog_product_entity_int` set value = 4 where attribute_id = 85
(assuming of course that the attribute_id was 85!)
Make sure you backup the database before you run it.
HTH,
JD