SELECT COUNT with LIKE gives strange result - mysql

I have a MYSQL-database with a table that has a mediumtext column. When trying to count the number of rows that has a specific word within that column, I get some strange result.
When I do this;
SELECT COUNT(*) AS antal FROM ot_pages where otp_ocr_raw LIKE '%vass%'
I get a count of around 860.
If I instead do a simple SELECT with the same question, like this:
SELECT * FROM ot_pages where otp_ocr_raw LIKE '%vass%'
I get a recordset with 70 rows back. Why is that? What would a correct SELECT COUNT-query look like to get a count of just 70?

Ok, I found the solution. The query contained letters like ÅÄÖ.
When I preformed a ordinary SELECT-query, those letters where correctly interpreted. But when doing a SELECT COUNT() those letters seems to have been interpreted by MySQL as AAO instead, hence giving a higher count than actual rows in the recordset.
The solution was to use utf8mb4 as charset, both in the script and the actual column. Now everything seems to work.

Related

how to obtain the explain result in mysql by code?

I planned to use rows in the explain result in replace of count(*). but I cannot get the rows number by code.
I have tried to treat the result as a table and select rows from it but it didn't work.

MySQL : Count returning double the number of entries when using distinct

So I do a count like so
select distinct count(prod.id) from product as prod....
I get back 175590
I do a select like so
select distinct prod.id from product as prod.... (rest of the query is exactly the same)
and I limit it. Now if I limit the query to return anything over the half way point it returns nothing. It appears as if count is returning double the number of entries each time.
Does anyone know of anything that may be causing this?
Thanks
Tracey
The DISTINCT keyword tells MySQL to strip the duplicate rows from the result set. Because SELECT COUNT(prod.id) returns a single row (I guess this, I cannot tell for sure until I see the complete query), adding DISTINCT in front of COUNT() does not change its behaviour in any way.
What you probably want is SELECT COUNT(DISTINCT prod.id) and that's a totally different thing. It removes the duplicate values of prod.id before counting them.
Your first query is counting how many prod.id's there are.
Your second query is showing all distinct prod.id's.
This is quite different.
If you were to do the second query without the distinct key word the number would be the same.

Mysql - Search for a word in json text and only give back a result if it occurs in more rows

Hy i want to search for a word in my table but i only want a result if the word occurred in more then 3 rows.
So basically my query is this, which partially works:
SELECT *
FROM `someTable`
WHERE `field_A` LIKE '%wordToFind%'
OR `field_B` LIKE '%wordToFind%'
This works just fine, in the result i will see all the rows that matched the query.
So the problem is it will return all the matched rows, for example if only one row was matched it will return only one, but i want this query to be limited to at least 5.
By that i mean i only want a returned result if the query match at least 5 fields.
I hope i was clear, thank you.
SELECT *
FROM `someTable`
WHERE (`field_A` LIKE '%wordToFind%'
OR `field_B` LIKE '%wordToFind%')
AND (SELECT COUNT(*)
FROM `someTable`
WHERE (`field_A` LIKE '%wordToFind%'
OR `field_B` LIKE '%wordToFind%')) >= 5
This might not be very efficient, as it may search for the rows twice, first for the subquery and then for the main query.

count(*) returning negative value

I have a strange problem and something that I haven't come across before. I have a mysql query like the one below
SELECT COUNT( * ) AS total_count FROM postcodes WHERE prefix='M1';
My query matches one row so I am expecting the value of total_count to be 1. However instead it is returning -1. Does anyone know why if would be returning a negative value?
According to this bug report, you need to run REPAIR TABLE.
Did you use ExecuteNonQuery instead of ExecuteScalar? Or in more general terms, did you read the row count instead of the result [in the first column of the first row]?
That's what I did to get a -1. Oops.

Delete rows with Sub Query?

I can't seem to get the SQL to work when using LIKE
DELETE FROM `customer_numbers`
WHERE number NOT LIKE (SELECT number FROM number_part)%
Basically delete all the rows from constomer_numbers table if number does not contain in number_part table
Example:
customer_numbers.number = 0559354544 and number_part.number = 05593 - it shouldn't delete it.. However if 05593 does not contain in customer_numbers.number then delete row from customer_numbers table.. It should match first 5 digits from number_part
You can't use not like with a list (in most databases, I'm pretty sure this is true in mysql).
Instead, you can use a correlated subquery:
DELETE FROM `customer_numbers`
WHERE not exists (SELECT number FROM number_part
where customer_numbers.number like concat(number, '%')
)
Your query is rather broken. In at least two ways:
Your subquery returns multiple rows, but is in a place where it looks like you expect a single result.
You need your LIKE string to be quoted.
2 is probably easy to fix. Try:
... WHERE number LIKE CONCAT((SELECT ... LIMIT 1),'%');
1 is really your problem, though. If you run your subselect as a single command, I expect you'll get multiple rows, right? How do you expect to treat a list of numbers (let's say, 1, 2, 3, 4, 5) as part of a LIKE string?
What I'm guessing you're hoping for is something like LIKE '1%' OR LIKE '2%' OR LIKE '3%'..., etc... No?
At any rate, if you can tell us more precisely what you're trying to do, we can probably help you solve your problem better.
As your question is worded, all I can say is: It doesn't work that way.
You named your fields numbers but it seems like they are character values because of the leading 0s so I am going to assume they are character fields.
If your part number is always going to be 5 characters long then you can do the following:
DELETE FROM `customer_numbers`
WHERE substring(number,1,5) NOT IN (SELECT number FROM number_part)
You can't do a LIKE on a numerical field.
You can't do an equality on a sub-select that isn't DISTINCT.
You must use "IN".
DELETE FROM `customer_numbers` WHERE number NOT IN (SELECT number FROM number_part)
How about something like this:
DELETE FROM customer_numbers WHERE number NOT IN (SELECT number FROM number_part)