Calculate the words number in a view in mysql - mysql

I have the following table structure
+-----------------+-------------+
| myID | text |
+-----------------+-------------+
| 3 | some text |
| 3 | other text |
| 5 | text |
+-----------------+-------------+
myID is not a unique ID. It can be repeated. Text is arbitrary strings.
I'm looking to create a view that returns the count of words in the text of the myID rows.
+-----------------+-------------+
| myID | count |
+-----------------+-------------+
| 3 | 4 |
| 5 | 1 |
+-----------------+-------------+
Well, I started out (I'm a newbie is mySQL) and did the following
SELECT
myid,
pagetext
FROM table
GROUP by myid
This does just the grouping. I think the next steps are
How to make pagetext returns the concatenation of the appropriate myID rows (I'm looking to limit the concatenation to 25 rows only).
How to count the number of words in the returned value.
Any ideas how to start?

You can find the number of words in a column (if you can assume there is 1 and exactly 1 space between words) with the following query:
SELECT
myId,
SUM(LENGTH(text)-LENGTH(REPLACE(text, ' ', ''))+1)
FROM table
GROUP BY myId
Source: http://www.mwasif.com/2008/12/count-number-of-words-in-a-mysql-column/

Related

Count all same comma separated value in one column mysql

I have 2 table and there's a column which contains comma separated value and I want to count all same value of that column, here's the sample table:
Client table
ID | Name | Procedure
1 | Joe | Samp1,Samp2
2 | Doe | Samp1,Samp2,Samp3
3 | Noe | Samp1,Samp2
Desire Output
Summary table ( For Procedure )
ID | NAME | COUNT
1 | Samp1 | 3
2 | Samp2 | 3
3 | Samp3 | 1
Now, do you have any idea or suggestion so i can make it happen ? like add new table or is this possible with single query ?
Try this query
SELECT LENGTH(COLUMN) - LENGTH(REPLACE(COLUMN, ',', '')) FROM TABLE_NAME

How to get titles(a row in a table) from MySql database so that the titles are in a particular order

I have the following query
SELECT id,title,image,address,serviceType FROM `ta` ORDER BY title LIMIT 0,20
It gives me results based on ordering of the titles.
The default ordering puts 'titles' starting with special characters first, then titles starting with numbers and then titles starting with alphabets.
But I want, first titles starting with numbers, then titles starting with alphabets and then titles starting with special characters.
Please help.
You can put case statement in your order by clause. Somthing like this:-
SELECT id, title, image, address, serviceType
FROM `ta`
ORDER BY CASE WHEN title LIKE '[0-9]%' THEN 1
WHEN title LIKE '[A-Z]%' THEN 2
ELSE 3 END
LIMIT 0,20
Hope this helps.
How to ask a question:
Hello, I have a table like this:
CREATE TABLE strings
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,string VARCHAR(12) UNIQUE NOT NULL
);
INSERT INTO strings (string) VALUES
('apple'),
('banana'),
('cherry'),
('*durian'),
('1eggplant'),
('10fig'),
('grapefruit');
SELECT * FROM strings;
+----+------------+
| id | string |
+----+------------+
| 4 | *durian |
| 6 | 10fig |
| 5 | 1eggplant |
| 1 | apple |
| 2 | banana |
| 3 | cherry |
| 7 | grapefruit |
+----+------------+
I would like a result set like this...
[PUT YOUR DESIRED OUTPUT HERE]
... where the rows are arranged by x, y z.
I tried this...
[PUT YOUR BEST EFFORT HERE]
... but that's not right because it's wrong in the following ways...

How to replace all special characters and spaces in a table column?

I have a table named "contacts" with below fields
id | phone_mobile
1 | 9988011223
2 | 00-12-123456
3 | 91-8876980987
4 | (91) 88990099777
I want a select query which will return below output
id | phone_mobile
1 | 9988011223
2 | 0012123456
3 | 918876980987
4 | 9188990099777
For a known set of characters you can use replace() function chaining something as
mysql> select replace(replace(replace('00-12-123456','-',''),'(',''),')','');
+----------------------------------------------------------------+
| replace(replace(replace('00-12-123456','-',''),'(',''),')','') |
+----------------------------------------------------------------+
| 0012123456 |
+----------------------------------------------------------------+
So in your case it could be
select
id,
replace(replace(replace(replace(phone_mobile,'-',''),'(',''),')',''),' ','') as phone_mobile
from table_name
If there is a long list of characters to be replaced then its better to use the application level to do the job since the chaining of replace becomes really messy.

How to select the first letter of each word from a table cell in MySQL?

How can I select the first letter of each word in MySQL using a query?
So this table
+----+----------------------------+
| id | str |
+----+----------------------------+
| 1 | Hello my name is MCEmperor |
| 2 | How are you doing? |
+----+----------------------------+
would return
+----+----------------------------+
| id | str |
+----+----------------------------+
| 1 | HmniM |
| 2 | Hayd |
+----+----------------------------+
I guess it's something with SUBSTRING and LOCATE and maybe I need a loop (to find all spaces or something)...
Is it possible within a query? How should I do that?
Maybe you could simply split by space? Use this stored proc : http://forums.mysql.com/read.php?60,78776,148332#msg-148332
You can then retrieve the first letters of each word and use GROUP_CONCAT in a GROUP BY Id to put the letters back into one line per initial text.
What you're looking for is a WHERE clause that matches only part of the data in the cell. You can do that like so:
SELECT str
from (table name)
WHERE str LIKE 'H%'

MySQL - COUNT before INSERT in one query

Hey all, I am looking for a way to query my database table only once in order to add an item and also to check what last item count was so that i can use the next number.
strSQL = "SELECT * FROM productr"
After that code above, i add a few product values to a record like so:
ID | Product | Price | Description | Qty | DateSold | gcCode
--------------------------------------------------------------------------
5 | The Name 1 | 5.22 | Description 1 | 2 | 09/15/10 | na
6 | The Name 2 | 15.55 | Description 2 | 1 | 09/15/10 | 05648755
7 | The Name 3 | 1.10 | Description 3 | 1 | 09/15/10 | na
8 | The Name 4 | 0.24 | Description 4 | 21 | 09/15/10 | 658140
i need to count how many times it sees gcCode <> 'na' so that i can add a 1 so it will be unique. Currently i do not know how to do this without opening another database inside this one and doing something like this:
strSQL2 = "SELECT COUNT(gcCode) as gcCount FROM productr WHERE gcCode <> 'na'
But like i said above, i do not want to have to open another database query just to get a count.
Any help would be great! Thanks! :o)
There's no need to do everything in one query. If you're using InnoDB as a storage engine, you could wrap your COUNT query and your INSERT command in a single transaction to guarantee atomicity.
In addition, you should probably use NULL instead of na for fields with unknown or missing values.
They're two queries; one is a subset of the other which means getting what you want in a single query will be a hack I don't recommend:
SELECT p.*,
(SELECT COUNT(*)
FROM PRODUCTR
WHERE gccode != 'na') AS gcCount
FROM PRODUCTR p
This will return all the rows, as it did previously. But it will include an additional column, repeating the gcCount value for every row returned. It works, but it's redundant data...