I am using ahoy gem in rails application to get the analytics data which is present in ahoy_events table i want to group the rows based on the id present in hash of properties column.
Table looks like this https://s10.postimg.org/j9e5ylvuh/data_table.png
Have tried query like Ahoy::Event.group("properties REGEXP '[{,]\"id\"[,}]' ") this returns only the first row.I know the syntax is correct but i am unable to find where i am getting wrong.Have search it a lot and tried different combination permutations of queries but could not find the correct one.Any suggesstions will be a great help.
Related
I'm trying to get zip codes from zip_id's which are internally stored in companies service table below screens will give you clear idea
I have wrote this query
companies service table
Please suggest me your valuable views . Thanks in advance.
As already mentioned your database scheme is not very well designed, it violates even 1st normal form. You'd need another table where you'd store serv_area_id and zip_code (with possibly multiple rows for a signle serv_area_id) and search within this table and eventually join your original table.
Nevertheless, in order to get the result you describe you cannot use the IN operator as it operates on a value and multiple values in a form of table (either explicit via nested SELECT or enumeration literal (val1, ..., valN)). I would try some string matching as illustrated below. However, consider it rather an ugly hack than correct solution(!)
SELECT zip FROM cities_extended WHERE (
SELECT GROUP_CONCAT(',', serv_are_zipcodes)
FROM company_service_areas WHERE ...
) LIKE concat('%(', id, ')%')
I have two table:
Table: Options
Options
Id xItems
- ItemA,ItemB,ItemC,etc
Table: Items
Items
Id
-
I am attempting to delete all Items rows that are not listed within Options.xitems
I attempted to execute the SQL statement
DELETE FROM items
Where items.id NOT IN (SELECT xitems FROM options)
However the problem is that multiple values are contained within XItems and I only managed to delete rows where Item.Id was the first or only value.
Would appreciate any kind help
EDIT: The following update added from the OP's post as an Answer.
The server is MySQL(tags edited accordingly) which allows one to enter an SQL statement below to execute against any database table or tables. I am a front end dev and get confused with this stuff.
John, I ran the code you posted. Here is the acutal code I applying against backedup test tables
DELETE FROM xbak514q_ecom_prodoptionsel
WHERE NOT FIND_IN_SET(xbak514q_ecom_prodoptionsel.id, (SELECT xprodoptionsel FROM xbak514q_ecom_prodoptions))
which returned the following error:
A problem was encountered while executing the SQL statement submitted.
The error was reported as: The MySQL extension encountered a problem
submitting an SQL statement. MySQL reported the error as: Subquery
returns more than 1 row
This database was configured by a software company who set up an e-comm site. The Items, Product options and selection items(add ons) are quite extensive. Should I consider reformatting the tables?
Again thanks for your kind help
have you tried using find_in_set()??
DELETE FROM items
WHERE NOT FIND_IN_SET(items.id, (SELECT xitems FROM options))
FIDDLE DEMO
NOTE:
find_in_set() is only for MySQL but since you have it tagged for both this may or may not be the solution. however the function looks for a comma separated list that is a single string or item and takes the first argument as the search string
RECOMMENDATION.
you should NEVER store data in the database as a comma separated list like that.. it causes HUGE issues in the future. please consider normalizing your database. if you want a way to do that just post a comment and I'll write up a query that will normalize it for you.
I've been trying to get this SQL query running for a while now and can't seem to get the last little bit going.
The backend database to all this data is a Drupal install with data spread out across a number of modules, so I need to do a lot of joining to get a certain view table set up that I need for a third-party application.
It's hard to explain the entire schema, but here's the sqlfiddle:
http://sqlfiddle.com/#!2/68df0/2/0
So basically, I have a userid which I map to a profile id through a join. Then I need to use that profile ID to pull the related data about that profile from two other tables. (there should only be one row with each pid in each of the other tables)
So in the end, I would like to see a table with username, othername, and key_id.
I got the first two pieces in there, but just can't seem to figure out how to join in the othername, and keep getting null.
The server is running MySQL.
Thanks guys!
LEFT JOIN other_name
ON profile_link.pid=other_name.pid;
I have a query on two columns from two different tables (connected by a left join in my query) and I want to order the search results by the occurence of the term I am looking for. I came up with this as my sorting variable in the statement, which might not be elegant, but works fine:
((LENGTH(table1.column)-LENGTH(REPLACE(lower(table1.column),lower('$term'),'')))/LENGTH('$term') AS sort_frequency
$term is my search term and at the end of the query I do this: ORDER BY sort_frequency DESC.
Now comes the difficulty: the calculation works fine for both tables separately, but when I want to connect the two by addition, the results of table2 always come in front of the results of table1 and nothing is ordered by occurence. My statement looks like this:
(((LENGTH(table1.column)-LENGTH(REPLACE(lower(table1.column),lower('$term'),'')))/LENGTH('$term')) + ((LENGTH(table2.column)-LENGTH(REPLACE(lower(table2.column),lower('$term'),'')))/LENGTH('$term'))) AS sort_frequency
I need this calculation, because the search results come from two different tables, but shall be ordered together on one page (let's say: one table is about images with certain keywords and the second table is about videos with certain keywords, once I searched for a specific keyword I don't care whether it is an image or video, I want the one that fits my keyword query most).
Do you have any idea why the calculation does not work? What is my mistake? I have tried adding/removing brackets, but that does not help.
Any help would be appreciated,
Have you tried using a UNION to combine the two table before searching for occurrences? My Suggestion is to use something along the lines of:
SELECT((LENGTH(newcolumn)-LENGTH(REPLACE(lower(newcolumn),lower('$term'),'')))/LENGTH('$term') AS sort_frequency FROM table1.column UNION table2.column AS newcolumn
I'm currently practicing using Sphinx, I've not far off done much, except the configuration what I'm trying to do. The sql_query key is leaving me somewhat confused what to put there, I read in the Sphinx documentation of sql_query but it doesn't seem to clear my mind from knowing what to do since I have many SELECTs in my web application, and I want to use Sphinx for my search and the SQL is often changed (upon user search filtering).
As of my search using MySQL, I want to integrate Sphinx to my web application, if the sql_key is not optional, do I have to expect to put the whole search SQL query into that field or do I pick out the necessary fields from tables to start a reindex?
Can someone point me to the right direction so I can get things going well with Sphinx and my web application.
sql_query is mandatory , it's run by sphinx to get the data you want to be indexed from mysql . You can have joins , conditions etc. , must be a valid sql query . You should have something like "SELECT id ,field1,field2,fieldx from table" . id must be a primary id .Each row returned by this query is considered a document ( which is returned by sphinx when you search ) .
If you have multiple tables ( that are very different by meaning - users , articles etc.) - you need to create an index for each .
Read tutorials from here : http://sphinxsearch.com/info/articles/ to understand how sphinx works .
You can create a sql query to get union set of records from the Database. If you do multiple table joining and query to select the best result set, you can do it with Sphinx too.
You may run into a few trouble with your existing table structure in the database.
Like :
Base table does not have integer primary key field
Create a new table which has two fields. One for the integer id field and the other field to hold the primary key of the base table. Do an inner join with that table and select the id field from that table.
Eg. SELECT t1.id, t2.name, t2.description, t2.content FROM table_new t1 INNER JOIN table_2 t2 WHERE t1.document_id = t1.thread_id INNER JOIN REST_OF_YOUR_SELECT_QUERY
The ta.id is for Sphinx search engine to do its internal indexing.
You filter data by placing WHERE clause and filtering
You can do that in Sphinx by setting filters dynamically based on the conditions.
You select and join different tables to get results
This also can be done by setting different sources and indexes based on your requirements.
Hope this would help you to get an understanding what you need to add and modify to start thinking how Sphinx search engine can be configured to your requirements. Just come here again if your need more help.