This question already has answers here:
FIND_IN_SET() vs IN()
(4 answers)
how to select all data whose input array found and not found in mysql
(1 answer)
Closed 6 years ago.
I've seen a lot of questions with a similar question. However, all the answers are misleading. Most answers say to use FIND_IN_SET and this seems incorrect to my question.
Below query:
SELECT
*
FROM
data_table
WHERE field_id IN ('618,622,626,773,776');
Would normally just show the first ID row. In this example being '618'
id data
--- ------
618 ....
How would one go about this without using any stored procedures or user defined functions? Displaying the following results:
id data
--- ------
618 ....
622 ....
626 ....
773 ....
776 ....
Note; '618,622,626,773,776' will always remain as a string. This is the problem here.
Most answers say to use FIND_IN_SET and this is completely incorrect to my question.
It is completely correct. If it didn’t work for you – then you just used it wrong.
SELECT * FROM data_table WHERE FIND_IN_SET(field_id, '618,622,626,773,776')
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I have two colums on mysql database table which holds float values. What I need is to get the sum of two colums and my mysql query is as follow
SELECT amntGot,commission, SUM(amntGot+commission) as amt from mytable;
Issue I got is the sum value goes wrong in the result
For example
amntGot = 4.6175 commission = 0.3825 Then amt goes to 4.999999821186066 . I expect result as 5 Which is (4.6175+0.3825)
I dont know why the result gives me 4.999999821186066 instead of 5
I could not find a dolution from StackOverflow, so any solution would be much appreciated,
I have attached screenshots of few results below
You can try this:
SELECT amntGot,commission, ROUND(SUM(amntGot+commission),0) as amt from mytable;
This question already has an answer here:
SQL statement is ignoring where parameter
(1 answer)
Closed 3 years ago.
Can someone tell me why this query isn’t working please?
$result = $connect->query( “SELECT *
FROM DBdata
WHERE catc !=‘121’
AND description LIKE ‘%$strm%’
OR ttl LIKE ‘%$strm%’
AND active = 1” );
THE PROBLEM - It IS LISTING ‘catc’ that = 121
I want the result it is giving, but don't want the catc items that equal 121
Thanks!
Your OR is likely what's messing things up.
SELECT * FROM HWpix
WHERE catc !='121'
AND
(description LIKE '%$strm%'
OR ttl LIKE '%$strm%')
AND active = 1
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
What I have with me?
A string with comma separated :
$str_states = "1,2";
I have a table with the following:
id event_name states
1 ABC 1,4,5
2 PQR 1,2,3
3 XYZ 3,4,5
What I want:
id event_name states
1 ABC 1
2 PQR 1,2
What I tried with a query:
SELECT id,event_name FROM events_table WHERE
FIND_IN_SET('1,2', states);
SELECT id,event_name FROM events_table WHERE
states IN ('51,58');
You can't really use FIND_IN_SET the way you want here. FIND_IN_SET searches for a single value against a CSV string. So, you would have to use OR here:
SELECT id, event_name
FROM events_table
WHERE FIND_IN_SET('1', inLocationId) > 0 OR FIND_IN_SET('2', inLocationId) > 0;
If your starting point for the input is 1,2, then you will have to tease apart the individual values in your app layer.
By the way, storing CSV unnormalized data in your SQL tables is bad practice. You would be better off to fix your design than to use my answer.
As a bonus, here is a more terse way to write your query using REGEXP:
SELECT id, event_name
FROM events_table
WHERE inLocationid REGEXP '[[:<:]](1|2)[[:>:]]';
But again, please fix your data model.
This question already has answers here:
MySQL query finding values in a comma separated string
(11 answers)
Closed 7 years ago.
I have records in user table in following manner
id name address keywords
1 thompsan paris 10,20,30
2 samson paris 10,20,30
3 Nilawa paris 10,20,30
4 Nalama paris 100,30,50
5 Nalama paris 100,300,20
I need to get the users who have the keywords of 10 or 20. I have written this query:
SELECT * from User where keywords REGEXP '[[:<:]]10|20[[:>:]]'
It does not give me the expected output. It should filter for id 10 or 20 and give me the output of record 1,2,3,5. record 4 is not matching here.
Why is it not working? Is there a better way to do this?
Try this,
SELECT *
FROM user
WHERE FIND_IN_SET('10', keywords) > 0 OR
FIND_IN_SET('20', keywords) > 0
FIND_IN_SET is a builtin function with MySQL
Redesign your database so that it's actually in 1NF and you won't have to deal with these headaches not to mention the horrible performance and bugs that it's bound to bring you down the line.
Since I know that you won't do that though, there's no need to use REGEXP at all, assuming that your string in keywords is actually consistent (and if it's not then you're screwed anyway). Just use LIKE:
SELECT -- We'll list out the columns, since we NEVER use SELECT *
id,
name,
address,
keywords
FROM
User
WHERE
',' + keywords + ',' LIKE '%,10,%' OR
',' + keywords + ',' LIKE '%,20,%'
This question already has answers here:
MYSQL REGEXP search in JSON string
(3 answers)
Closed 7 years ago.
I have three records in mysql:
1. a:7:{s:10:"state_name";s:11:"West Bengal";s:7:"city_id";a:40:{i:0;s:4:"1166";i:1;s:4:"1454";i:2;s:4:"1455";i:3;s:4:"1456";i:4;s:4:"1458";i:5;s:4:"1459";i:6;s:4:"1460";i:7;s:4:"1461";i:8;s:4:"1463";i:9;s:4:"1464";i:10;s:4:"1465";i:11;s:4:"1466";i:12;s:4:"1468";i:13;s:4:"1469";i:14;s:4:"1470";i:15;s:4:"1471";i:16;s:4:"1473";i:17;s:4:"1474";i:18;s:4:"1475";i:19;s:4:"1476";i:20;s:4:"1478";i:21;s:4:"1479";i:22;s:4:"1480";i:23;s:4:"1481";i:24;s:4:"1483";i:25;s:4:"1484";i:26;s:4:"1485";i:27;s:4:"1486";i:28;s:4:"1488";i:29;s:4:"1489";i:30;s:4:"1490";i:31;s:4:"1491";i:32;s:4:"1493";i:33;s:4:"1494";i:34;s:4:"1495";i:35;s:4:"1496";i:36;s:4:"1498";i:37;s:4:"1499";i:38;s:4:"1500";i:39;s:4:"1501";}s:6:"gender";s:4:"male";s:3:"age";s:0:"";s:12:"category_ids";s:2:"68";s:16:"product_type_ids";s:2:"30";s:18:"min_purchase_price";s:1:"0";}
2. a:7:{s:10:"state_name";s:11:"West Bengal";s:7:"city_id";a:8:{i:0;s:4:"1465";i:1;s:4:"1466";i:2;s:4:"1467";i:3;s:4:"1471";i:4;s:4:"1475";i:5;s:4:"1476";i:6;s:4:"1478";i:7;s:4:"1479";}s:6:"gender";s:0:"";s:3:"age";s:4:"0-30";s:12:"category_ids";s:2:"58";s:16:"product_type_ids";s:0:"";s:18:"min_purchase_price";s:2:"50";}
3. a:7:{s:10:"state_name";s:11:"West Bengal";s:7:"city_id";a:1:{i:0;s:4:"1475";}s:6:"gender";s:6:"female";s:3:"age";s:4:"0-30";s:12:"category_ids";s:2:"58";s:16:"product_type_ids";s:0:"";s:18:"min_purchase_price";s:3:"100";}
with the column name conditions_serialized.
Now I am trying to write a sql query to fetch all the records having city_id 1475.
My code is like :
SELECT `main_table`.*, `rule_coupons`.`code` FROM `salesrule` AS `main_table` LEFT JOIN `salesrule_coupon` AS `rule_coupons` ON main_table.rule_id = rule_coupons.rule_id AND rule_coupons.is_primary = 1 WHERE (conditions_serialized regexp 'city_id";a:[0-9]*:{.*(i:[0-9]*;s:[0-9]*:"1475";)}')
But by this only 2 records are displaying 2nd and 3rd, not the first one.
Could you please check my query and rectify that why it is not displaying all?
Thanks in advance.
You are looking explicitly for 1475
city_id";a:[0-9]*:{.*(i:[0-9]*;s:[0-9]*:"**1475**";)}
change it to this instead to get records from the 3 records. Note: from 3 to 5 digits
city_id";a:[0-9]*:{.*(i:[0-9]*;s:[0-9]*:"\d{3,5}";)}