How do I replace multiple strings from one column in MySQL? - mysql

I am working on a multiple language database.
Unfortunately, some of strings was mistakenly
inputted into the wrong language column.
I have a glossary for those strings,
but the replacement task is huge (over 3,000).
I cannot finish the work with the Query I used before.
*UPDATE
tableName
SET
column = REPLACE(column,'BereplaceString','replaceString')
WHERE
locale = "XX" and
description like “%BereplaceString%"
It is possible to make the replacement task into one command?
(I can use excel to combine for the replace string,
but don't want to execute the command for a 1000 times)
Thanks for helping in advance!
Now:
ID Column A Column B
(Langauge) (Description)
1 EN red pomme
2 FR apple rouge
3 EN yellow citron
4 FR lemon jaune
Expected Outcome:
ID Column A Column B
(Langauge) (Description)
1 EN red apple
2 FR pomme rouge
3 EN jaune citron
4 FR lemon yellow

Related

How to design this many-to-many database?

I want such a database design. It serves to store a list of words in one language, i.e. English, and their translations in another language, i.e. Spanish. So initially I have a table English (id,word,isTranslated).
1 i false
2 love false
3 you false
4 hate false
5 him false
...
"isTranslated" is a boolean indicating whether this word has a translation or not yet.
In the Web front end, each time a number of words are displayed in an html page from the table English. For example:
2 love
3 you
4 hate
A user then clicks each of these words and submits the translated words in a web form and they are stored into the Spanish table. Unlike the English table, the Spanish table starts with zero records. It gets populated gradually through the html form.
I am thinking the Spanish table should have the same structure as the English does;
Spanish (id,word,isTranslated)
So that I can have another associative table EnglishSpanish(English_ID, Spanish_ID), which stores the translated pairs in two tables. It's a many-to-many relationship. The purpose of this table is to facilitate retrieval of the counterparts of a word in a language.
Does this make sense? The trouble I am having is, how to populate the other two tables Spanish and EnglishSpanish gradually, as users submit their translations over time? The first table English is pre-loaded.
Thank you for your insights and help.
I once designed a dictionary (one-to-one) relationship, as in real world, you will pick up only one best translation for each English phrase:
id, term, lang, english_id
1 I En 1
2 Love En 2
3 You En 3
4 我 Cn 1
5 爱 Cn 2
6 你 Cn 3
7 ichi De 1
...
Above Designed also works for one-many relationship
If you want it as many-many relationship, you need 2 tables:
id, term, lang
1 xyz en
2 ...
id1, id2
1 2
1 3
2 5
...

COUNTIF for rows which contain a given value in another column

My table lists every character from all 5 of George R. R. Martin's currently published A Song of Ice and Fire novels. Each row contains a record indicating which book in the series the character is from (numbered 1-5) and a single letter indicating the character's gender (M/F). For example:
A B C
1 Character Book Gender
------------------------------
2 Arya Stark - 1 - F
3 Eddard Stark - 1 - M
4 Davos Seaworth - 2 - M
5 Lynesse Hightower - 2 - F
6 Xaro Xhoan Daxos - 2 - M
7 Elinor Tyrell - 3 - F
I can use COUNTIF to find out that there are three females and three males in this table, but I want to know, for example, how many males there are in book 2. How could I write a formula that would make this count? Here is a pseudocode of what I'm trying to achieve:
=COUNTIF(C2:C7, Column B = '2' AND Column C = 'M')
This would output 2.
I'm aware that this task is far better suited to databases and a SELECT query, but I'd like to know how to solve this problem within the constraints of a LibreOffice Calc spreadsheet, without using a macro. Excel-based solutions are fine, so long as they also work in Calc. If there's no solution that uses COUNTIF, it doesn't matter, so long as it works.
I worked it out, thanks to a prompt by assylias. The COUNTIFS formula produces the result I want by counting multiple search criteria. For example, this formula works out how many male characters are in Book 1 (A Game of Thrones).
=COUNTIFS($A$2:$A$2102, "=1", $L$2:$L$2102, "=M")

join six tables with date ans market column repeating in all the tables.

I have six tables in my database with 45 columns each,with date ans market column repeating in all the tables. 1 million records in it. my date and market values are not unique and is repeating so how can i join these tables together ?
any help ?
For eg:
table 1:
date market col1 col2
1 may India san det
1 may USA lif det
2 may India lif san
table 2 :
date market col3 col4
1 may india san go
1 may USA dif dic
2 may ind det san
RESULT AS
date market col1 col2 col3 col4
1 may India san det san go
1 may USA lif det dif dic
2 may India lif san det san
I tried joining two tables using the query like :
select * from cre,dur where cre.period = dur.period and cre.market = dur.market;
But once i try creating a table its not working .
I joined six tables by hard coding all the column names but is not the right way i think ?
Any HELP ?
You can use in below way-
select cre.*,dur.* from cre,dur where cre.period = dur.period and cre.market = dur.market;
Note: Even your requirement is not much clear to me but as per my understanding you skip cre.period=dur.period then also you can get same results as per your data.
Even better to keep only required column, for this you can follow below steps-
Step1: Export below query results.
select * from table1 limit1;
Step2: Copy all columns from header line and remove unwanted columns.
Further your table structure does not seems good, you should keep one table's reference in another table through parent and child id.

Choosing terms to insert into database that allows record to be queried easy later

I have a text file that I parse each month and insert the data into my database. The text file looks like this.
------------------------------------------------------------------------------------------------------------------------------------------------------------
BLK. NO 410 | 1 2| 3 4 5 6 7 | 8 9| 10 11 12 13 14 |15 16| 17 18 19 20 21 |22 23| 24 25 26 27 28 |29 30| 31 1 2
BLK 88.35 |SA SU| MO TU WE TH FR |SA SU| MO TU WE TH FR |SA SU| MO TU WE TH FR |SA SU| MO TU WE TH FR |SA SU| MO TU WE
CR. 88.35 |194 / 159 | |194 / 159 | |194 / 159 | |194 / 159 | |239
OFF 14 : : VER DFW TRC DFW X : : VER DFW TRC DFW X : : VER DFW TRC DFW X : : VER DFW TRC DFW X : : CUU DFW
C/O 06.00 TAFB 216.15 194=/1410/1055/0740, 159=/0900/1700/1400, 239=/1845/1945/0155
EM3 25.55 ERD 42.00 EM4 20.40
-----------------------------------------------------------------------------------------------------------------------------
This text file has from 100 to 300 of these records (we refer to it as a "line of flying") and I use it to determine my flight schedule each month. I am parsing this file just fine but the company added a new wrinkle.
This "line" can be for a pilot or a flight attendant. Until recently each line could be for a captain or First Officer. But now some files have captain lines and first officer lines. The flight attendant file could have 1 or two flight attendant positions or more depending on the type of aircraft flown.
I need to create a new column in my database that reflects this new information. Once the lines are saved to the database, they are only searched, never updated again. I don't want to add a new table just to keep track of this information. I want to add a new column to the existing table. Here is my strategy.
When I parse the text file, I will keep track of the positions available. If it is for Captain, I will save "CA" to the database. If it is first officer, I will save "FO". If it is for both a captain and First officer, I will save "CAFO". For flight attendants I will save a number "1" for 1 flight attendant poistion. For each additional poistion, I will just increment it one number. This would allow for expansion later if larger aiplanes required more flight attendant poistions.
So to recap, I will save one the following for each database record.
"CAFO"
"CA"
"FO"
"1"
"2"
Now I will need to access the database and retrieve these records for my web program. If a captain wants to retreive records He will want only captain lines which would be "CAFO" or "CA". And just the opposite for first officers.
When I do the query, I found that I could use the mySQL command SUBSTRING to just grab the first 2 or last 2 characters.
for captain lines
SELECT Line_number,html_text FROM lines WHERE SUBSTRING(Crew_type,1,2)="CA"
This would just get the first two beginning characters from the record and it would match both cases.
For First officers I start at the end of the string and grab last two characters.
SELECT Line_number,html_text FROM lines WHERE SUBSTRING(Crew_type,-2)="FO"
For flight attendants I could compare the number.
If a flight attendant only wanted lines with 1 position.
SELECT Line_number,html_text FROM lines WHERE Crew_type = "1"
Or if they want all possible flight attendant positions
SELECT Line_number,html_text FROM lines WHERE Crew_type >= "1"
I would set the column type to ENUM with the values I listed above.
My question is there a better way to approach this problem? I will only be inserting once, but will be retreiving a lot. Will the SUBSTRING command slow down the queries too much?
SELECT Line_number,html_text FROM lines WHERE SUBSTRING(Crew_type,1,2)="CA"
will be a performance killer, as it calculates the substring for all records, not only those used. You want
SELECT Line_number,html_text FROM lines Crew_type in ("CA", "CAFO")
Ditto for FO.
So somewhere in your PHP iw would define
$matches=array(
'CA'=>'"CA","CAFO"',
'FO'=>'"FO","CAFO"',
'FA'=>'"1","2"'
);
and then use
$match=$matches[$crew_type];
$sql="SELECT Line_number,html_text FROM lines Crew_type in ($match)";
with $crew_type being one of 'CA','FO','FA'

Need help with Sql Server 2008 and Full Text Searching

I've setup a FTS on a single field, in a single table.
Field: Name NVARHCHAR(350) NOT NULL
Now, when i search for the following
1 ave
10 ave
i don't get back the results i expect.
Firstly, the search query 1 ave is transformed into "1*" AND "ave*". Now i run my CONTAINS(..) query...
SELECT FooId, Name
FROM [dbo].[Names]
WHERE CONTAINS(Name, #SearchQuery)
Then, along with the correct results, i also get these incorrect results back...
2 Ave (a couple of entries .. but they are all unique entires).
So, how did this get retrieved? there is no 1* in that piece of text? Its like .. the number is ignored?
Also - and this is important - i've removed an reference to a stop list AND rebuilt the catalog.
Hmm. I'm so confused. anyone have any suggestions?
The "1" can occur anywhere within the full text search indexed column it doesn't have to be directly before (or even before) the "Ave", is there a 1 somewhere else in that row?
Full text indexing will find derivitaves of words - like if you search for RUN, it might find RUNNING, RAN, RUN, etc.
I wonder if its deciding that 2 is near 1, and returning that as a near match. You should try switching your query to a CONTAINSTABLE query so that you can also evaluate the RANK to determine which of the answers is a closer match. You could then decide on a threshold and filter out any rows that don't meet your criteria as to how close of a match they are.
EDIT: Its not doing the inflection thinking 1 is near 2. I ran a test query on a sample table that looked like this...
PK Name
1 1 ave
2 10 ave
3 2 ave
4 12 avenue
5 13 avenue
6 100 ave.
7 200 ave
8 210 avenue
Here's the query I ran...
select *
from Table_1
where contains(name, '"1*" and "ave*"')
And here's the results I get...
PK Name
2 10 ave
4 12 avenue
5 13 avenue
6 100 ave.
The interesting thing here is that the first record in the table isn't found. (I ran this on SQL 2008 Dev edition). Based on those results (where nothing starting with 2 was found) - I'd double-check your query. Maybe post the full text of your entire query, including where the search variable is being set.