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

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.

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%';

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'

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

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

Select query with length issue [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Most efficient T-SQL way to pad a varchar on the left to a certain length?
I have the customer table where the customerID is of length:10
but there are few customer where the length is 3 or 5
ex:
3445
34
789
7800
but the output should be like the following, I need to prefix zero here if length is less than 10
0000003445
0000000034
0000000789
0000007800
Script to get this data is:
select customerID from customer
Try the following code:
Since you are using sql server 2008, you could use replicate function to add zeros to the value..
Select ltrim(right(replicate(0,10) + <column>,10))
from your_table
You can use in this way...
SELECT RIGHT('0000000000' + RTRIM('3445'), 10)
In your case,
SELECT RIGHT('0000000000' + RTRIM(customerID), 10) AS New_CustomerID
FROM Customer
Try this
SELECT RIGHT('0000000000'+`customerID`, 10) FROM `customer`