MySQL - count where id contained in a string [duplicate] - mysql

This question already has answers here:
MySQL search in comma list [duplicate]
(5 answers)
Closed 9 years ago.
My table is called "asd"
I have this schema:
id | date | content |
AUTO_INC DATETIME LONGTEXT
now assuming content = 45,67,89,3,45,5
how do i search COUNT() in this table WHERE content CONTAINS 89 for example ?
i tryed SELECT COUNT() FROM asd WHERE content IN(89); but i got no results.

You can just use FIND_IN_SET
SELECT COUNT(*) FROM `table` WHERE FIND_IN_SET(89,`content`);
P.S. You need to read about many-to-many relations.

Try this
SELECT COUNT(*)
FROM ASD
WHERE ',' + content + ',' like '%,89,%'

can you please try like this....i have seen something you want in this link http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/
..hope it helps
SELECT SUM(IF(content = 89, 1,0)) AS `MATCHED VALUE`,
COUNT(content) AS `total`
FROM asd

SELECT COUNT(*) as cnt FROM `asd` WHERE `content` LIKE '%,89,%' OR `content` LIKE '89,%' OR `content` LIKE '%,89';
Check the SQL Fiddle : http://sqlfiddle.com/#!2/151c7/2/0

SELECT COUNT(*)
FROM ASD
WHERE content LIKE '%,89,%' OR
content LIKE '%,89' OR
content LIKE '89,%'
%,89,% - to match 89 in the middle of the content
%,89 - at the end of the content
89,% - at the beginning of the content

Related

How to count the length of column seperated by ',' after using group_concat [duplicate]

This question already has answers here:
How to count items in comma separated list MySQL
(6 answers)
Closed 3 years ago.
I have a table looks like below:
ID path
| 1 YouTube,Newsletter,Social
| 2 YouTube,Newsletter
| 3 YouTube
Now I want to create a column to count the length of the path column. such as below:
ID path count weights
| 1 YouTube,Newsletter,Social 3 0.33
| 2 YouTube,Newsletter 2 0.5
| 3 YouTube 1 1
How do I do this?
I have tried JSON_LENGTH but couldn't get the command working.
PS. essentially I'm trying to replicate a query in PostgreSQL:
' select user_id, channels, 1.0 / array_length(channels, 1) as weights
from (
// ... query for marketing_channels as before)'
I am using MYSQL.
select d.email_entry_id
,d.channels
,JSON_LENGTH(d.channels)
from (
select email_entry_id
,group_concat(attribution_string order by visit_date asc separator ',' ) as channels
from database) d
error message: Error Code: 1370. execute command denied to user 'yb'#'%' for routine 'company.JSON_LENGTH'
Hope the question is clear enough. let me know if i need to clarify anything.
If I followed you correctly, you could simply extend the logic of your existing query (which, by the way, seems to be missing a GROUP BY clause). Instead of querying the aggregated data, it would be simpler to start from the original data, like:
SELECT
email_entry_id,
GROUP_CONCAT(attribution_string ORDER BY visit_date SEPARATOR ',' ) as channels,
COUNT(*) as `count`,
1/COUNT(*) as weight
FROM database
GROUP BY email_entry_id
There is a very common trick to achieve such outcome, demonstrated by following query
SELECT ID, PATH,
(LENGTH(PATH) - LENGTH(REPLACE(PATH, ',', ''))) + 1 COUNT
FROM DATABASE /* OR WHATEVER IS THE TABLE NAME */
The result

How to select rows that contain specific number in MySQL? [duplicate]

This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
I have this table and want to select rows that contain exactly "22".
id field
1 22
2 22,24,78
3 1,22,347
4 2,21,22
5 22,222
Select above rows, not below.
6 222
7 21,23
8 220,322
The REGEXP operator comes in handy here:
SELECT *
FROM yourTable
WHERE field REGEXP '[[:<:]]22[[:>:]]';
We can also try using FIND_IN_SET:
SELECT *
FROM yourTable
WHERE FIND_IN_SET('22', field) > 0;
If all else fails, we can use LIKE, but it takes slightly more heavy lifting:
SELECT *
FROM yourTable
WHERE CONCAT(',', field, ',') LIKE '%,22,%';
But in general, it is bad practice to store CSV (comma separated values) in your database tables. It would be better to store each field value on a separate rows, e.g. use this:
id field
1 22
2 22
2 24
2 78
...
select * from where field like '%22%';

MySQL strip ' on where queries

I have a large table which contains, or not, records that have ' tags like (martin's, lay's, martins, lays, so on).
Actually to search the client can be write exactly text, for example: martin's, to search all records that contains "martin's" but it is complicate, then, I need the client can to search by "martins" or "martin's".
This is a simple example:
A mysql table like:
ID | Title
---------------
1 lays
2 lay's
3 some text
4 other text
5 martin's
I need a sql query to search by lays or lay's and both need show me a Result like:
ID | Title
---------------
1 lays
2 lay's
I'm tried with many post solutions but I cant do that :-(
Appreciate any help.
Just remove the single quote:
select t.*
from t
where replace(t.title, '''', '') = 'lays';
To search if the word contains:
select t.*
from t
where replace(t.title, '''', '') LIKE '%lays%';

Use of right string function [duplicate]

This question already has answers here:
Any way to select from MySQL table where a field ends in certain character/number?
(7 answers)
Closed 7 years ago.
I need to list a few details from a certain table, where two columns end with a certain number, and I must use the RIGHT string function.
Using MySQL Workbench
So far I have:
Select dog_id, Cat_id, Place
FROM EVENT_ENTRY
In the columns dog_id and cat_id, there is data such as 501, 502, 401, 301, 201, 101, 91 etc.
I need to list all of the data from both dog_id and cat_id that ends with 1, using the right string function.
Any help would be greatly appreciated. Cheers.
SELECT dog_id, Cat_id, Place FROM EVENT_ENTRY
WHERE RIGHT(dog_id, LEN(dog_id)) = 1 AND RIGHT(cat_id, LEN(cat_id))
SELECT dog_id, Cat_id, Place
FROM EVENT_ENTRY
WHERE RIGHT(dog_id,1)='1' and RIGHT(cat_id,1)='1';
If these are int columns (or any other integer data type for that matter), it's better to test if modulu 2 = 1, like this: WHERE Dog_Id % 2 = 1 AND Cat_Id % 2 = 1
If these are string columns (nchar, nvarchar etc.), you can use the right function, like this: WHERE RIGHT(Dog_Id, 1) = '1' AND RIGHT(Cat_Id, 1) = '1',
or use like with a wildcard, like this: WHERE Dog_Id LIKE '%1' AND Cat_Id LIKE '%1'

Search strings that have a specific prefix xxx* [duplicate]

This question already has answers here:
In MySql, find strings with a given prefix
(2 answers)
Closed 9 years ago.
In my data base, I have a field date_ID with a strings as follows:
----------------------------------
+ date_ID +
----------------------------------
111-11-333 555+33+22 -00000
111-22-333 555+33+22 -00569 -- > element 1
111-05-333 555+33+22 -00789
111-22-333 555+33+22 -00008 -- > element 2
111-22-333 555+33+22 -00001 -- > element 3
111-22-111 555+33+22 -00001 -- > element 4
----------------------------------
How can I select all date_ID strings, that contains 111-22 as the prefix?
Table is dynamic. I'm new to mySql, so please help.
UPDATED QUESTION
I'm doing it in iOS, so I'm confused with the syntax too. So, pls help me to get the string as the form below.
NSString *passedPrefix = #"111-22";
[NSString stringWithFormat:#"select * from my_table WHERE date_ID LIKE \"%#\"",passedPrefix]
// How to add that last '%' here?
Just use the LIKE operator:
SELECT * FROM mytable WHERE date_id LIKE '111-22%'
In normal SQL you would use:
SELECT * FROM myTable WHERE date_ID LIKE '111-22%'
But as you've asked how to use this in iOS stringWithFormat, I think this is how:
WHERE date_ID LIKE '%#%%'
According to my source, you don't need to escape with backslashes.
I know nothing about iOS development, I literally did a Google search for you.