Mysql Regexp with array - mysql

I am getting an array of category id eg. ['1','2','3']
and in my database table it is stored as 1_abc eg : category : 1_Sameer
Now it is easy if i get one value
ie. WHERE category like 1_%;
But since this is an array how can i use like or regex to compare with the database values

In this case you can do as many queries as you have array elements and merge these results together if needed to get single collection of elements.
$results = new Collection();
foreach($ids as $id) {
$categories=Category::where("id","like","$id_%")->get();
$results=$results->merge($categories);
}

you have to do looping for that
for($i=0;$i<=count($array_result)-1;$i++)
{
$result=mysql_query("select * from table_name where category like '{$array_result[$i]}_%' ");
}

Related

Comparing List of regex to a string

i have question i don't know better approach to do it in mysql. I have a table in mysql with list of regex's each regex represent a company order number
i want to be able to compare a number to that list to get which company this number belongs to. the lazy way is to list all the regex in php and then using loop to get the company , but i want to do this using the power of mysql .
Like #Mech mention this might be vague.
i will try to explain it more :
I have two tables table with actual regex pattern in plain text in a column like "^[8]{1}[0-9]{10}$"
and this pattern belong to a company , there is more than 500 regex patterns .
Thank you.
Here you go #BM2ilabs. A function as requested :)
carrierID(88888141234);
function carrierID($ordernum) {
// create a connection to your db here
// fetch data needed for loop
$sql = "SELECT regex, carrier_id FROM `company_tbl_from_image`";
// fetch results
$results = $conn->query($sql);
// loop through $results
foreach ($results as $result) {
// individually check against each regex in the table
$regex = $result[regex];
// find first instance of $regex, where the $ordernum is unique, there should only be one match
if (preg_match('/'.$regex.'/', $ordernum)) {
$carrier_id = $result[carrier_id];
break; // remove break to show other matches
}
}
// check if $carrier_id is empty
if ($carrier_id <> "") {
echo $carrier_id;
} else {
echo "No carrier ID found.";
}
}
MySQL only option. Just search this:
SELECT carrier_id FROM `company_tbl_from_image` WHERE 'order number' REGEXP regex

Symfony Doctrine: Search in simple_array

I got values stored in my database column field as value1,value2,value3,value4, so a simple_array column.
So i'm using Doctrine to make a search using this:
$searchQuery = $this->getDoctrine()
->getRepository('AppBundle:Ads')
->createQueryBuilder('p')
->andWhere("p.vals <= :value2")
->setParameter('value2', $request->query->get('value2'));
->orderBy("p.creationtime", 'DESC');
So expecting value2 is in the 2nd position of a simple array like value1,value2,value3, how can i ask QueryBuilder to select the second value in the string?
I think this query try to get all the values in p.vals, results are not right, shound select just one.
How can I select eg. the 2nd value in p.vals?
I believe you cannot access nth item of an array column using pure Mysql since the data is serialized, in order to do it I'd create a simple function
public function getItemFromArray(array $array, $index)
{
return isset($array[$index]) ? $array[$index] : null;
}
And if you want to find item with condition use
array_filter()

Why is count(id) resulting 3 dimensional array?

Im using this direct query method in cakephp to get a count (against the norms of MVC).
$count = $this->History->query("SELECT count(id) AS x FROM ratings WHERE id='3' AND employee='28'");
But it turns out that, i need to use $count[0][0]['x'] to get the value.
Why isnt it available at $count['x']?
in your case it will return it in a general way: 0 = iterate over all models, and again 0 since you dont have a specific model name). your result will always be in $count[0][0]['fieldname'] then.
it is highly recommended to always use the wrapper methods if possible.
why are you not using the cakephp database wrapper methods as documented?
$count = $this->History->find('count', array('conditions'=>array('id'=>3, 'employee'=>28));
?
it would result in the expected output x
Simply, its because the function
$this->History->query(....);
does not return a single dimensional array. I will suggest you to create a helper function, which extracts the single dimensional array and return it.
function single_query($query) {
$result = $this->History->query($query);
return $result[0][0];
}
then use it
$count = $this->single_query("SELECT count(id) AS x FROM ratings WHERE id='3' AND employee='28'");

mysql commands difference and example

I'm new in mysql language and can not understand difference between procedures and functions, can anyone answer in which case should be use this routines ?
Also have some example => I've table named "data" and columns named "id"(primary key) , "local". this local includes multiple exactly same data. I want to search every id of this "data" (and after result manipulate with it) table which local equal to (for instance) 'something'
Please answer in this question ... Thanks
Functions:
select <function>(column) from table where <condition>;
Procedure:
call <procedure>( param0, param1 );
To get your result:
select * from <table> where data like "%something%";
I suppose that you use php with mysql.
your query should be
$result = mysql_query("SELECT * FROM data WHERE local='something'");
while($row = mysql_fetch_array($result))
{
// here you manipulate with your data
// for example:
echo $row['id'];
}

Use an sql "CONVERT" stored procedure in a Magento collection

I want to add a "CONVERT" stored procedure like this:
SELECT id, value, CONVERT(value, DECIMAL) AS ordred_value FROM test ORDER BY ordred_value;
to this collection query :
$collection = $this->getAssociatedProductCollection($product)
->addAttributeToSelect('*')
->addFilterByRequiredOptions()
->setPositionOrder()
->addStoreFilter($this->getStoreFilter($product))
->addAttributeToFilter('status', array('in' => $this->getStatusFilters($product)))
->addAttributeToSort('my_attribute', 'DESC');
for the purpose of ordering the associated products by my custom attribute "my_attribute" that have numeric values in text fields.
Thanks for help.
I'm not sure it's the bast solution, but you can do it like this:
<?php
$collection->getSelct()->columns(array('converted_value' => 'CONVERT(e.my_attribute, DECIMAL)'));
It will add a new column to result. Next sort the collection using it's select as above and the order method.