Multiple Word Text-Search on MySQL,Returning Number Before Specified Word - mysql

I wanna ask, I have a table that contain a column that contain text.For example :
Table name : Table_Books ||
Table Columns : Text, Total_Books, Total_Paper
Example text in column Text : 146 books were published, 82 books and
46 papers were released
Now,I want to get the number in front of every word books and paper in each row of column Text.
The result of the query in Total_Books will be : 228 (because
we find 146 and 82 before word books in column Text) The result of the
query in Total_Paper will be : 46 (because we find 46 before
word papers in column Text)
Can anyone help me how the query is? I dont know how to check if there's more than one books/paper in text of each row.
Thank you so much for the help :D

Related

Need to ignore last two values in a lists column using Hive HQL

I have a column which contains all the values in lists.
Column A|Column B
AAA |1 2 45 67 89
BBB |16 25 36 45 89 63
CCC |52 63 98 41 22 66
Here in the above table, column B contains string values which are actually lists.
I need to ignore the first two and the last two values in Column B.
I tried using split function where i can ignore first two values. But ignoring last two values is the challenge as I have different sized lists.
The code which I used is:
select distinct column_A,column_B,split(column_B,'\\s')[2] AS ign_first_val,
split(column_B,'\\s')[-2] as ign_last_val
FROM Xyz
Is there any easy way to ignore first two and last two values in a list using HQL?
You should be able to use regexp_extract:
select regexp_extract(column_B, '^\\s*(\\d+\\s+){2}(.*?)(\\s+\\d+){2}\\s*$', 2)
The first part of the regex skips the first two values, and the last part skips the last two values, leaving just the middle part to be extracted into group 2 which is what is returned by the expression.
Here's a demo of the regex working on regex101.com

Column 1 with names and Column 2 with values

Let's say I have a Text document. There are two columns. Column 1 contains a list of names while column contains a list of value relating to those names. The problem is column 1 may have same names repeating on different rows. This is not an error though.
For ex:
Frank Burton 13
Joe Donnigan 22
John Smith 45
Cooper White 53
Joe Donnigan 19
What are the ways to organize my data in a way that I would have column 1 with unique data names and column 2 with the values summed together relating column 1? What can I do if I have these data in excel?
For ex:
Frank Burton 13
Joe Donnigan 41
John Smith 45
Cooper White 53
Thanks a bunch!
In mySQL you could write a query similar to...
Select col1, Sum(col2) FROM TableName group by col1
In Excel you could use a pivot table to group the information together
Insert Pivot table, select range enter values as in image below.

how to push data down a row in sql results

I would like help with sql query code to push the consequent data in a specific column down by a row.
For example in a random table like the following,
x column y column
6 6
9 4
89 30
34 15
the results should be "pushed" down a row, meaning
x column y column
6 null or 0 (preferably)
9 6
89 4
34 30
SQL tables have no inherent concept of ordering. Hence, the concept of "next row" does not make sense.
Your example has no column that specifies the order for the rows. There is no definition of next. So, what you want to do cannot be done.
I am not aware of a simple way to do this with the way you are showing the table being formatted. If your perhaps added two consecutively numbered integer fields that provide row number and row number + 1 values, you could join the table to itself and get that information.
After taking a backup of you table:
Make a PHP function that will:
- Load all values of Y into an array
- Set Y = 0 (MYSQL UPDATE)
- load the values back from PHP array to MYSQL

How do I delete the first 3 characters in mysql?

How might I tell mysql to..
delete the first 3 characters from mytable.title only where the first 3 characters are numbers 01-10 followed by a space?
Example..
Delete any record in the 'title' column that looks like this:
01 California
09 Texas
10 Idaho
Leave the records alone that don't start with a number and space (such as these):
California
Texas
Idaho
I have several thousand records that I want to remove these preceding numbers from. Thoughts?
UPDATE my_table SET title=SUBSTRING(title, 4) WHERE title REGEXP '^[0-9][0-9] ';

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.