I have a MySQL table which contains comma-separated values like this:
first row=(A,AB)
second row=(AC, AE)
I want to select the rows which have A in their set not AC OR AE. I am using symfony2 in my code.
$query = $query->andWhere("FIND_IN_SET('".trim($tag)."',advNews.advNewsTags)");
Its Return Error: Expected known function, got 'FIND_IN_SET'
But if i use query in sqlyog like
select * from advice_news where adv_news_type = 'news' AND FIND_IN_SET ('sdf',adv_news_tags)
its work fine.
Tell me any solution i will use FIND_IN_SET in symfony2.
You need to add FunctionNode to Doctrine and Symfony. I had the same problem with SOUNDS LIKE. You can find my solution at http://piotrpasich.com/full-text-searching/ in SOUNDEX chapter.
Related
when I want to get data from a database in native PHP I use queries but in laravel, you don't write queries you use their model and their functions to get the data for example:
ex::where('name','test')->get();
in native PHP:
select * from ex where name='test'
and my issue is I don't know how to use and like this query:
select * from ex where name='test' and id='5'
I searched and looked thru the documentation but no answer was found.
Just put another where, its really simple just put it like this
ex::where('name', 'test')->where('id', 5)->get();
//or if you want directly the instance and not a collection
ex::where('name', 'test')->find(5);
If you want to use the OR operator you can also do something like this
ex::where('name', 'test')->orWhere('id', 5)->get();
More in documentations
There is no 'AND' with where in Laravel Eloquent.
You have to write another 'where' with the required condition.
Another way is: run queries using the DB facade. The DB facade provides methods for each type of query: select, update, insert, delete, and statement.
Ex. DB::statement("UPDATE teachers SET price = ".$price." where id=".$id." AND status='A'");
Do not forget to use DB
From my names table containing values id, firstName, lastName I want to take out all rows, but from the firstName I just want the first letter.
I know I can make custom mySql query with doctrine but was wondering is there a more....well docrtine way to do such things (and other value select operations).
What could be the way to do it with solely doctrine?
An option to get the full string and then cut it in php is out of the question.
I think you're looking for DQL Function substring(). If you're using query builder, You can do $qb->expr()->substring(string, start,end). If I remember correctly, first character's position is 1 not 0.
Refer: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html
I have the following MySQL query:
SELECT * FROM users WHERE name LIKE '%jack%'
I want it to order by how much it's like jack so
jack
jacker
majack
I can also use PHP.
I think you can accomplish what you want by using full text search function in mysql. Your query will be like this:
SELECT name, MATCH (name) AGAINST ('jack') as score
FROM users ORDER BY score DESC;
There are some conditions you need to take into consideration when using full search text:
The mysql engine should support this functions. I think InnoDB 5.6 and MYISAM 5.5 support that.
You have to add a FULLTEXT index in your table definition.
You can see a working demo here: http://sqlfiddle.com/#!9/72bf5/1
More info about full search text in MySQL here: http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html
Also, here is a working example I wrote on PHP using similar_text and sorting array functions: http://ideone.com/UQpBFk
Hope it helps!
I don't think you can do this kind of thing with plain SQL. The first thing you need to do is define what it means for two strings to be similar, for which there are various metrics. You should probably pick one of those and write a stored procedure to sift through the data.
As every one mentioned, you can't achieve this using normal way other than using full text search but if you know all the different pattern before hand then you can use FIELD function to achieve something which resemble a approx result like
SELECT * FROM users WHERE name LIKE '%jack%'
ORDER BY FIELD (name,'jack','jacker','majack')
Try something like this:
$query = mysql_query("SELECT * FROM users WHERE name LIKE '%jack%' ORDER BY name ASC ");
while($fetchdata=mysql_fetch_array($query))
{
echo $fetchdata["name"] ;
}
I want to look for duplicate entries in my table and show all of them. How can I find all duplicated values in one column using Propel ORM?
Well, this question suggests using counts... you could replicate that query in Propel (I think) with this:
$results = TableNameQuery::create()
->select(array("id", "field", "COUNT(*)"))
->groupBy("field")
->having("COUNT(*) > ?", 1)
->find();
Of course, this gets a little hairy, so you might just want to use straight SQL if Propel fails you.
(For reference, here's the SQL:)
SELECT field, COUNT(*)
FROM table_name
GROUP BY field
HAVING count(*) > 1
I want to filter result of my SQL query. I want to select everything that has some specific text in some column.
Example:
SELECT * FROM categories WHERE (name
has 'abc' values in it's value ex.
MyabcCategory)
Also maybe it is not very good idea to do that in query, maybe it is better to get all and then filter array instead? But I don't know how to do that aether.
Use LIKE with % wildcard:
SELECT * FROM categories WHERE name LIKE '%abc%'
This will give you all the records that have abc somewhere in them.
You can learn more about it here :)
You want to use the LIKE operator, with % to match any character before and after your specific word :
select *
from categories
where name like '%abc%';
But note that doing so, MySQL will scan each line of the table, every time the query is executed... which might not be great if you have a lot of data.
If you're searching for some kind of text, you might either want to :
Use a FULLTEXT index, if you're working with MyISAM tables.
Or, use a solution that's separated from MySQL, with a specific indexing/search engine, such as Solr.
SELECT * FROM categories WHERE name LIKE '%abc%'