Sphinx mysql " WHERE IN " for string colums - sphinx

Is there any way to perform the following query in sphinx index.
SELECT * FROM search_index WHERE MATCH('sea*') AND object_type IN ('news', 'videos') LIMIT 0, 7
This query doesnt work and throw the following error
sphinxql: syntax error, unexpected QUOTED_STRING, expecting CONST_INT or '-' near ''news', 'videos') LIMIT 0, 7 '

I think very latest version of sphinx (released a few days ago) supports that, but not totally sure.
But you could also just use fields rather than attributes, then can do it in the full-text query
SELECT * FROM search_index WHERE MATCH('sea* #object_type (news|videos)') LIMIT 0, 7

Related

Postgres SQL escape keyword from column name during SQL select statement

I am new to postgres and one of my genius colleague managed to name a column in postgres table as follow
How far are you able to commute - less than 11 - 20 km
I am trying to retrieve this column in select statement as follows:
Select schema.tablename.How far are you able to commute - less than 11 - 20 km from schema.tablename
Obviously this will throw error as there are keywords such as
"are", "to"
How can I escape this nonsens? I have already searched a lot and tried with "columnname" and it isnt working.
Thank you for helping in advance!

Problem with a query using GROUP BY in PostgreSQL

I'm using the query tool in PGAdmin 4.20 and trying the following query:
select * from metadatavalue group by resource_id order by resource_id;
And I'm getting the following:
ERROR: column "metadatavalue.metadata_value_id" must appear in the
GROUP BY clause or be used in an aggregate function LINE 3: select *
from metadatavalue group by resource_id order by re...
^ SQL state: 42803 Character: 176
The thing is that in another table, I use the same syntax and works:
select * from metadatafieldregistry group by metadata_field_id order by metadata_field_id;
Also, I'm not getting all the entries from a same resource_id, only a few. Could these two problems be related?
Please, help!
Thank you in advance.

simple sql LIMIT statement

Can anyone tell me if this is a valid sql statement?..
SELECT * FROM myTable WHERE foo="bar" ORDER BY foo_date DESC LIMIT 0, 5
I've searched online but the difficulty is a) inexperience and b) finding an example that contains the components I'm trying to assemble.
I want the statement to select ALL from the table where condition1 is true, then order the results by descending date, then LIMIT the returned rows to those specified...
many thanks to anyone that can give me a pointer in the right direction.
Scott
SELECT * FROM myTable WHERE foo="bar" ORDER BY foo_date DESC LIMIT 0, 5
...is fine - runs in phpMyAdmin no problem. I should have tested there before posting on SO - apologies.

Filtering sphinxql group by query by count

Does anybody know how filter sphinxql group by query by count?
For example I have query like:
SELECT collection_id, count(*) as cnt
FROM mobile_content
WHERE collection_id != 0
GROUP BY collection_id
And I want to take to result only rows that have cnt greater than 5. If I make something like this, I get an error:
SELECT collection_id, count(*)
FROM mobile_content
WHERE collection_id != 0 AND count(*) > 5
GROUP BY collection_id;
ERROR 1064 (42000): sphinxql: Aggregates in 'where' clause prohibited near '5 GROUP BY collection_id'
I remember that in older versions I used #count to filtering group by results.
My current sphinxsearch version is 2.1.5. Is it possible to filter results by count in this version?
My current sphinxsearch version is 2.1.5. Is it possible to filter results by count in this version?
No.
The HAVING clause, was added in 2.2.1-beta
http://sphinxsearch.com/docs/current.html#sphinxql-select

PostgreSQL -must appear in the GROUP BY clause or be used in an aggregate function

I am getting this error in the pg production mode, but its working fine in sqlite3 development mode.
ActiveRecord::StatementInvalid in ManagementController#index
PG::Error: ERROR: column "estates.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "estates".* FROM "estates" WHERE "estates"."Mgmt" = ...
^
: SELECT "estates".* FROM "estates" WHERE "estates"."Mgmt" = 'Mazzey' GROUP BY user_id
#myestate = Estate.where(:Mgmt => current_user.Company).group(:user_id).all
If user_id is the PRIMARY KEY then you need to upgrade PostgreSQL; newer versions will correctly handle grouping by the primary key.
If user_id is neither unique nor the primary key for the 'estates' relation in question, then this query doesn't make much sense, since PostgreSQL has no way to know which value to return for each column of estates where multiple rows share the same user_id. You must use an aggregate function that expresses what you want, like min, max, avg, string_agg, array_agg, etc or add the column(s) of interest to the GROUP BY.
Alternately you can rephrase the query to use DISTINCT ON and an ORDER BY if you really do want to pick a somewhat arbitrary row, though I really doubt it's possible to express that via ActiveRecord.
Some databases - including SQLite and MySQL - will just pick an arbitrary row. This is considered incorrect and unsafe by the PostgreSQL team, so PostgreSQL follows the SQL standard and considers such queries to be errors.
If you have:
col1 col2
fred 42
bob 9
fred 44
fred 99
and you do:
SELECT col1, col2 FROM mytable GROUP BY col1;
then it's obvious that you should get the row:
bob 9
but what about the result for fred? There is no single correct answer to pick, so the database will refuse to execute such unsafe queries. If you wanted the greatest col2 for any col1 you'd use the max aggregate:
SELECT col1, max(col2) AS max_col2 FROM mytable GROUP BY col1;
I recently moved from MySQL to PostgreSQL and encountered the same issue. Just for reference, the best approach I've found is to use DISTINCT ON as suggested in this SO answer:
Elegant PostgreSQL Group by for Ruby on Rails / ActiveRecord
This will let you get one record for each unique value in your chosen column that matches the other query conditions:
MyModel.where(:some_col => value).select("DISTINCT ON (unique_col) *")
I prefer DISTINCT ON because I can still get all the other column values in the row. DISTINCT alone will only return the value of that specific column.
After often receiving the error myself I realised that Rails (I am using rails 4) automatically adds an 'order by id' at the end of your grouping query. This often results in the error above. So make sure you append your own .order(:group_by_column) at the end of your Rails query. Hence you will have something like this:
#problems = Problem.select('problems.username, sum(problems.weight) as weight_sum').group('problems.username').order('problems.username')
#myestate1 = Estate.where(:Mgmt => current_user.Company)
#myestate = #myestate1.select("DISTINCT(user_id)")
this is what I did.