Why does this SQL query return a blank result?
select
webs.title,webs.disc,webs.logo,webs.id
from
webs
JOIN rel ON rel.webid=webs.id
JOIN catagory on catagory.id=rel.catid
where
webs.app!=0
AND MATCH (webs.title,webs.city,webs.state,webs.url,catagory.catname) AGAINST ('+".$_GET['search']."' IN BOOLEAN MODE)
This is a query for multiples searches in multiple tables and fields.
I also want to order the result by the best matching word.
If you want to use "MATCH() ... AGAINST", first create fulltext index on the field which you want in match.
Related
I've built an internal DB / Search Engine for art creatives. I'm trying to create a search criterion where you can query one column in the database and also search several columns in the database for a phrase search using FullText Search. The example of a search query might be: November {and} Black Friday. November would search for creatives matching the created_for column and the black friday would search headline, subheadline and additional_text columns with a fulltext search. Any ideas of how to accomplish this would be really helpful!
SELECT
(SELECT * FROM headlines WHERE created_for = '$searchString' AND image_slug <> '')
(SELECT *, MATCH(headline) AGAINST('$fullText' IN BOOLEAN MODE) AS MultiScore, MATCH(subheadline, additional_text) AGAINST('$fullText' IN BOOLEAN MODE) AS MultiSecondScore
FROM `headlines`
WHERE MATCH(headline, subheadline, additional_text) AGAINST('$fullText' IN BOOLEAN MODE))
I've tried adding a UNION statement before the second Select statement, but I get an error message saying the columns don't match. Not sure what I've got wrong here, but thanks in advance for your help!
Use AND in the WHERE clause.
SELECT *, MATCH(headline) AGAINST('$fullText' IN BOOLEAN MODE) AS MultiScore, MATCH(subheadline, additional_text) AGAINST('$fullText' IN BOOLEAN MODE) AS MultiSecondScore
FROM headlines
WHERE created_for = '$searchString'
AND image_slug <> ''
AND MATCH(headline, subheadline, additional_text) AGAINST('$fullText' IN BOOLEAN MODE)
UNION would get results that match either of the criteria, not both of them. And when you use UNION, both subqueries have to return the same number of columns -- you would have to add extra columns to the first query to match the MultiScore and MultiSecondScore columns of the first query.
I have this following query which join multiple tables
SELECT targa,
registrazioni.turno as turno,
conduttori.nome as nome,
conduttori.cognome as cognome,
spese_importo,risparmio,km, SUM(spese.spese_importo) AS totspese
FROM registrazioni LEFT JOIN conduttori
ON id_conduttore=conduttori.id
LEFT JOIN spese
ON registrazioni.id=spese.id_registrazione
WHERE dataora='$data';
when I added this SUM(spese.spese_importo), I started having a small issue, and even if the query shouldn't have for me any matching result, I obtain as result an empty row where every field is NULL.
Of course if I remove that SUM from my query, it does work again and I don't have any rows as result of the query
How could I solve it and check if there are results not null?
I've tried via mysql to add the condition
WHERE targa IS NOT NULL
(targa is just one field I've randomly choosen between the different fields they are all NULL)
but it didn't clear the row, and via php I was using the condition
mysqli_num_rows($result)==0
but now that I always have at least 1 row it doesn't work.
any other ideas for checking it before fetching the rows?
I think you want to group your results.
Try to add this at the end of your query :
GROUP BY registrazioni.id
From http://www.w3schools.com/sql/sql_join_left.asp:
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the >right table (table2). The result is NULL in the right side when there is no match.
Does including sum(spese.spese_importo) create a situation where you would get a match on the left side of the join but not on the right side - and have those NULLs?
I have a table in a database with records containing keywords as well as other data. What would be a logical way to create a search function that would allow people to search based on keywords, and order results based on the number of matched keywords?
Mysql provide FULLTEXT search options. Check this link mysql full text search. These search results will be sorted according to best match. it also has support for boolean mode and NATURAL LANGUAGE MODE(default). You need to add FULLTEXT index on search column.
Here is the query that will work for you.
SELECT *, MATCH (ab,cd) AGAINST ('sample text' IN BOOLEAN MODE) AS relevancy
FROM table_name
WHERE MATCH (ab,cd) AGAINST ('sample text' IN BOOLEAN MODE)
ORDER BY relevancy DESC;
hey, i`m just wondering what I should do to this query to have it not return any duplicate items.
SELECT tag FROM tags WHERE MATCH (tag)
AGAINST ('$sql_items' IN BOOLEAN MODE) LIMIT 5
Ive tried a bunch of different stuff but I cant seem to get it to work :( I`m using php
DISTINCT is your friend :) (OR GROUP BY)
SELECT DISTINCT tag FROM tags WHERE MATCH (tag)
AGAINST ('$sql_items' IN BOOLEAN MODE) LIMIT 5
What about using a distinct in your select clause ?
A bit like this, I'd say :
SELECT distinct tag
FROM tags
WHERE MATCH (tag)
AGAINST ('$sql_items' IN BOOLEAN MODE)
LIMIT 5
Consider
SELECT tag
FROM tags
WHERE MATCH (tag) AGAINST ('$sql_items' IN BOOLEAN MODE)
GROUP BY tag
LIMIT 5
This assumes you may need to select other fields from tags. If you just need tag, then DISTINCT tag may be all it takes.
I'm trying to get fulltext searches to be sorted by relevance in a
Doctrine_RawSql query.
This code will perform the search:
$q = new Doctrine_RawSql();
$q->select('{p.*}')
->from('cms_page p')
->where('match(p.content) against (?)', $user_query)
->addComponent('p', 'CmsPage p');
This will execute. I would like the results to be sorted by relevance
The real sql would have to look something like:
select
p.id,
match(p.content) against (?) as score
from
cms_page as p
order by
score desc;
So I need to get that match ... against clause in the select... I think.
My crapshoot guess at accomplishing this was:
$q->select("{p.id}, match({p.content}) against ('$escaped_user_query') as score")
->from('cms_page p')
->orderBy('score DESC')
->addComponent('p', 'CmsPage p');
That doesn't work. Any pointers?
Thanks in advance!
According to the MySQL fulltext natural language search docs:
When MATCH() is used in a WHERE clause, as in the example shown earlier, the rows returned are automatically sorted with the highest relevance first. Relevance values are nonnegative floating-point numbers. Zero relevance means no similarity. Relevance is computed based on the number of words in the row, the number of unique words in that row, the total number of words in the collection, and the number of documents (rows) that contain a particular word.
Does this not work for you?
You can read more about natural fulltext search in MySQL here.