Why do I get a MySQL error from this query?
SELECT *, MATCH($SW) AGAINST(reb, keb, gloss) as Score
FROM dictionary
WHERE MATCH($SW) AGAINST(reb, keb, gloss)
ORDER BY Score DESC;
I'm trying to test Full Text search but I can't create a working query it seems like.
It would be nice if you gave the error message, but I think I can guess the fix. You forgot to quote the strings:
SELECT *, MATCH('$SW') AGAINST(reb, keb, gloss) as Score
FROM dictionary
WHERE MATCH('$SW') AGAINST(reb, keb, gloss)
ORDER BY Score DESC;
Related
I want to write a query to retrieve rows who have a max version for their particular groups
For eg:
English Basic has 4 versions(test_id:1,2,4,7). But I want to retrieve the one with the max version
with this I also want to retrieve other such records of max version
I want this result
Mom Basic 20 1(version)(id:5)
Mom Intermediate 30 1(version)(id:6)
English Basic 20 5(version)(id:7)
Thanks and Let me know if you have any doubts
Here is what I've tried:
I got the SQL query:
SELECT *, MAX(`version`) FROM `test` GROUP BY `test_name`,`sub_category`,`test_type`
But I am not able to properly write it in laravel (throws syntax error)
Here are my laravel tries:
$test = Test::selectRaw('*, MAX(version) GROUP BY `test_name` ')->get();
$test = Test::selectRaw('*')->groupBy('sub_category')->toSql();
and so on
P.S. Don't worry about the format of commas or quotes as I tried all the combinations with this. Just not able to figure out what is the write code
I did not try this one. But I think you can do this. What you meant by unique is not clear.
Test::where('version', Test::max('version'))->get();
Please try this,
Mysql
select test_type,test_name,sub_category,time,max(version) as version
from table_name group by test_name,sub_categoryorder by version ;
Laravel
$result=DB::select(DB::raw("select test_type,test_name,sub_category,time,max(version) as version
from table_name group by test_name,sub_categoryorder by version"));
Check Fiddle
$maxVersion = Test::orderBy('version', 'desc')->first(); // gets the whole row
Here is the solution I found:
$test = DB::select(DB::raw("
SELECT * FROM test a
INNER JOIN (SELECT test_type, test_name,sub_category, MAX(version) AS maxversion
FROM test GROUP BY test_type,test_name,sub_category) b
ON a.test_type=b.test_type
AND a.test_name=b.test_name
AND a.sub_category=b.sub_category
AND a.version=b.maxversion"));
There is no way to easily translate this into eloquent if you could do it then please post it here. Also you need to make sql strict in your config->database file as false to run this as it has some issue with group by
So far, I have taken 3 tables and joined them together. What I want to do is display the Last 100 entries (DESC) in ASC ORDER according to the timestamp in the column Posted.
This is as far as I could get: http://sqlfiddle.com/#!9/e2771/1
In addition, if there is a more efficient way to do this in PhP and not MYSQL, I'm all for that. I've tried looking, but haven't been able to find anything that works.
You just need one more level of sort:
select t.*
from (<your query here>) t
order by posted;
I'm working on a page which displays set of data from mysql.
I wanted to know is there any way to avoid displaying a data with same name?
for example if i have 2 skills in database as PHP and php then it should only display either one of those .
Use group by in your SQL query to group the skills and appear only one.
If the column name is skill append the following line to the query
GROUP BY LOWER(skill);
Use DISTINCT keyword.
e.g SELECT DISTINCT skill FROM skills
Database is case-insenstive (at least MySQL), so php and PHP will be treated as duplicates.
Hence, the problem gets solved.
try this query
SELECT DISTINCT column_name,column_name FROM table_name;
SELECT DISTINCT col1 FROM skills
DISTINCT keyword is used to return only distinct (different) values.
Some things in lecture and in my lab assignment were not explained very well. I am having trouble displaying the correct information.
Here is the database info, simply a reference for you to help me.
The database tables info
This is the query that I am trying to execute
The postgresql php select statement
This results in this SQl error being throwned
Connected to database!
Query failed: ERROR: column "city.name" must appear in the GROUP BY clause or be used in an aggregate function LINE 3: city.name ^
Now if I do add city.name into the GROUP BY clause, it returns 4096 rows! I dont want that to happen, the results have to be group by country name which is 232 rows. I simply want to display the country name, city name, and the city with the highest population in that country. City name is throwing me off, Im guessing there is a more complicated more syntax heavy solution.
Thanks for any help you can provide.
-Tom Reese
You need something like this:
select
country.name,
city.name,
mp.maxpop
from
lab6.country,
lab6.city,
(select
country_code,
max(population) as maxpop
from
lab6.city
group by
country_code
) mp
where
country.country_code=mp.country_code and
country.country_code=city.county_code and
mp.maxpop=city.population
notes:
This can give you more result/county.
Your original query doesn't work because in ansi sql you can't return only aggregated or group by expressions from a "group by" query. (As the error mentions)
I have the following query which is used in order to do an auto-complete of a search box:
SELECT *, MATCH (screen_name, name) AGAINST ('+query*' IN BOOLEAN MODE) AS SCORE
FROM users
WHERE MATCH (screen_name, name) AGAINST ('+query*' IN BOOLEAN MODE)
ORDER BY SCORE DESC LIMIT 3
I also have a FULL TEXT index on screen_name & name (together). When this table was relatively small (50k) this worked great. Now the table is ~200k and it takes seconds(!) to complete each query. I'm using MySql MyISAM. Is this reasonable? What directions might I check in order to improve this as surely it doesn't satisfy the needs of an auto-complete query.
MYSQL Match against is really slow, you should look into alternatives like Sphinx Search Server.