Having trouble while searching in mysql via REGEXP - php

I am implementing search in my project so I have to search data which will be in any order so I'm following this question. But my query was not working
my php query is
$cat="(?=.*Women)(?=.*Rings)";
$sql="select * from tbl_jewellery where categories RLike '$cat';";
when I change regex to Women.*Ring|Ring.*Women
It works fine but (?=.*Women)(?=.*Rings) approach is easy and can be used for multiple words just adding them.

MySQL does not implement (? syntax in REGEXPs. I think MariaDB 10.0.5 does.
For a significant subset of such queries, you could use a FULLTEXT index on that categories with this:
MATCH(categories)
AGAINST('+women +rings' IN BOOLEAN MODE)
That says both "words" occur, without limitations on order or proximity.

Related

elasticsearch php nested aggregations

I tried searching an trying.
I need to group the nested fields.
The sql query is as follows:
SELECT p_application_category,
Sum(p_recv_bytes) as download,
p_date
FROM ZLog2 $w
group by p_application_category;
I solved the problem with elastic sql plugin. It makes regular sql statements available in elastic.

select from a select statement in php & mySQL

I am trying to add filters to a DB search. I have a search that takes some text and tries to find items with that text in the title. I also have a price range filter. That code is below and works just fine
$sql = "SELECT * FROM items where title LIKE '%". $title ."%' AND price > '". $price1 ."' AND price < '".$price2."' Limit 70";
Now I am trying to more and more filters. Is there a select from the above code's output? I don't want to just keep making a longer SELECT statement with tons of if statements. I'd prefer to take the output of the previous select and refine that with another select. Is this possible?
EDIT 1 Context:
Users are the ones entering the information. This is for searching the items on my site.
There's no other useful way than adding lots of different conditions to your WHERE cause, if you use plain SQL. It is possible to use several nasted SELECT statements in your query, but this makes your code neither any more readable nor faster.
A more elegant solution is the usage of query objects or another form of object-oriented query abstraction (e.g. ZendDB).
You can use some of the mysql string functions like INSTR(), MATCH which will make your life a little easy and also help the readability of the code.
You can also use REGEXP and NOT REGEXP for pattern matching . The list of string functions are here.

Query a Query - MySQL and PHP

I was recently trying to do a project*, which caused me to ask this question. Although since then I've found an alternative solution, I am still curious if what I envisioned doing is, in any way, possible.
Essentially, I am wondering if there is anyway to perform a MySQL query on a MySQL query result in php. For example:
$result = mysql_query("SELECT * FROM foo WHERE bar=".$barValue);
AND THEN, be able to perform multiple queries on $result:
$newResult = mysql_query("SELECT * FROM $result WHERE otherBar=".$barValue);
OR
$otherNewResult = mysql_query("SELECT * FROM $result WHERE otherOtherBar=".$barValue." ORDER BY foobar ASC");
AND so on and so forth...
I realize that I could append the original query with my new WHERE statements and ORDER BYs, but that causes my to query the database unnecessarily and it prevents me from writing more objected oriented code (because I can't pass around a result to be queried, but rather have to requery the database in every function...)
Any advice, pieces of code, frameworks, or ramblings appreciated.
*BTW, my project was having to query a large database of people for people born in certain age groups and then query those age groups for different demographics.
Edit
No, writing a custom function to query the database is not worth the object-orientation (and modifiability) it would give me
You could do a nested query in the same SQL query and keep PHP out of it:
'SELECT * FROM (SELECT * FROM foo WHERE bar="something") AS q1 WHERE q1.bar2 = "something else"'
The question has already been answered. However following explanation will help someone who might be interested in knowing the details of it.
What are Nested query / subquery:
Subqueries are also known as nested queries. A subquery is a SELECT statement within another statement. MySQL supports all SQL standards and additionally provides MySQL specific features.
Why should I use Subquery:
Subquery is structured and it is possible to isolate each parts of statement
Subquery is more readable that complex joins and unions
Subquery provides alternative means to perform action which otherwise would require complex joins and unions
What Subquery returns:
A subquery can return a single value, a single row, a single column, or a table. These are called scalar, column, row, and table subqueries.
Reference: http://dev.mysql.com/doc/refman/5.7/en/subqueries.html
http://www.w3resource.com/sql/subqueries/nested-subqueries.php

how to write Regular Expression in MySQL select queries?

I tried this expression /\b(word\w*)\b/i to compare a word against the list of other words to find the duplicates. I used preg_math_all() and it worked fine. I wanted to do the same thing but this time check against the words retrieved from mysql database. This is what i wrote
SELECT * FROM table WHERE word = /\b(word\w*)\b/i
this didnt worked.
Your query should look like:
SELECT * FROM table WHERE column REGEXP '/\b(word\w*)\b/i'
MySQL regex syntax is a different from PHP's, you don't need the delimiters at the start and end.
SELECT * FROM table WHERE word REGEXP '\b(word\w*)\b'
You can't use the i flag for case insensitivity the same way as in PHP, as in MySQL whether the regex is case sensitive or not depends on whether the column collation is a case sensitive one or not.

MySQL: escape space from query

Is there a way using MySQL, to query a value containing a space, but as some sort of escape character? It's for an instant search engine I'm building (I'm trying to incorporate special search strings such as quotes to search for exact string).
cheers
select * from sometable where somefield like '%oh no%';
Full-text indexes are what you should use. They handle all sorts of key words/characters (such as quoting a string to get exact match, -someword for saying someword can't be in the result and +someword for saying someword has to be in the result) without having to do anything special in your code (other than changing your query a little). The database will do the search for you and return the most relevant results at the top of the query. It is really quite easy to get going too.
Mysql Manual
Using full-text searching (implementation help)

Categories