SQL Query - multiple search terms, two columns - sql

I'm wondering if someone could provide some guidance on an SQL query to return results from two columns using a varying amount terms. It's SQLite3 in Node.js. This is a music library search function, so I want to be able to type something like "Justin Peaches" or "Bieber Peaches" or "Justin Bieber Peach" which would return the same result. Currently, my SQL query is:
SELECT * FROM songs WHERE track LIKE '' or artist LIKE '' ORDER BY track LIMIT 12;
table: "songs"
track: Peaches (Clean) artist: Justin Bieber (featuring Daniel Caesar)
Obviously this only allows searching of artist OR song rather than both at the same time. I would like to be able to enter individual words of the artist, song, or a combination of both. It's very similar to the way you'd search pretty much any other music library wherein you can use a combination of artist, track, or some of each.
I'm not sure if this is something that can be accomplished directly in an SQL query or if there needs to be some additional handling in advance. Any help would be appreciated!

Related

How to get information of a specific Wikidata ID using SPARQL query?

I know how to find all movies in Wikidata. So, I get a list of IDs like "Q18644475". If I try to get all film info for every film in the database in one query, I get a timeout. So, I decided to get the film info in individual queries.
Is there an SPARQL query to get the title and release year(optionally all possible info) for this ID?
I find the hoops you have to jump through to use an entity ID in SPARQL to be quite nuts.
If anyone else stumbles across this question then an alternative is to download the complete entity information in JSON format.
For example:
https://www.wikidata.org/wiki/Special:EntityData/Q18644475.json

Learning ExecuteSQL in FMP12, a few questions

I have joined a new job where I am required to use FileMaker (and gradually transition systems to other databases). I have been a DB Admin of a MS SQL Server database for ~2 years, and I am very well versed in PL/SQL and T-SQL. I am trying to pan my SQL knowledge to FMP using the ExecuteSQL functionaloty, and I'm kinda running into a lot of small pains :)
I have 2 tables: Movies and Genres. The relevant columns are:
Movies(MovieId, MovieName, GenreId, Rating)
Genres(GenreId, GenreName)
I'm trying to find the movie with the highest rating in each genre. The SQL query for this would be:
SELECT M.MovieName
FROM Movies M INNER JOIN Genres G ON M.GenreId=G.GenreId
WHERE M.Rating=
(
SELECT MAX(Rating) FROM Movies WHERE GenreId = M.GenreId
)
I translated this as best as I could to an ExecuteSQL query:
ExecuteSQL ("
SELECT M::MovieName FROM Movies M INNER JOIN Genres G ON M::GenreId=G::GenreId
WHERE M::Rating =
(SELECT MAX(M2::Rating) FROM Movies M2 WHERE M2::GenreId = M::GenreId)
"; "" ; "")
I set the field type to Text and also ensured values are not stored. But all I see are '?' marks.
What am I doing incorrectly here? I'm sorry if it's something really stupid, but I'm new to FMP and any suggestions would be appreciated.
Thank you!
--
Ram
UPDATE: Solution and the thought process it took to get there:
Thanks to everyone that helped me solve the problem. You guys made me realize that traditional SQL thought process does not exactly pan to FMP, and when I probed around, what I realized is that to best use SQL knowledge in FMP, I should be considering each column independently and not think of the entire result set when I write a query. This would mean that for my current functionality, the JOIN is no longer necessary. The JOIN was to bring in the GenreName, which is a different column that FMP automatically maps. I just needed to remove the JOIN, and it works perfectly.
TL;DR: The thought process context should be the current column, not the entire expected result set.
Once again, thank you #MissJack, #Chuck (how did you even get that username?), #pft221 and #michael.hor257k
I've found that FileMaker is very particular in its formatting of queries using the ExecuteSQL function. In many cases, standard SQL syntax will work fine, but in some cases you have to make some slight (but important) tweaks.
I can see two things here that might be causing the problem...
ExecuteSQL ("
SELECT M::MovieName FROM Movies M INNER JOIN Genres G ON
M::GenreId=G::GenreId
WHERE M::Rating =
(SELECT MAX(M2::Rating) FROM Movies M2 WHERE M2::GenreId = M::GenreId)
"; "" ; "")
You can't use the standard FMP table::field format inside the query.
Within the quotes inside the ExecuteSQL function, you should follow the SQL format of table.column. So M::MovieName should be M.MovieName.
I don't see an AS anywhere in your code.
In order to create an alias, you must state it explicitly. For example, in your FROM, it should be Movies AS M.
I think if you fix those two things, it should probably work. However, I've had some trouble with JOINs myself, as my primary experience is with FMP, and I'm only just now becoming more familiar with SQL syntax.
Because it's incredibly hard to debug SQL in FMP, the best advice I can give you here is to start small. Begin with a very basic query, and once you're sure that's working, gradually add more complicated elements one at a time until you encounter the dreaded ?.
There's a number of great posts on FileMaker Hacks all about ExecuteSQL:
Since you're already familiar with SQL, I'd start with this one: The Missing FM 12 ExecuteSQL Reference. There's a link to a PDF of the entire article if you scroll down to the bottom of the post.
I was going to recommend a few more specific articles (like the series on Robust Coding, or Dynamic Parameters), but since I'm new here and I can't include more than 2 links, just go to FileMaker Hacks and search for "ExecuteSQL". You'll find a number of useful posts.
NB If you're using FMP Advanced, the Data Viewer is a great tool for testing SQL. But beware: complex queries on large databases can sometimes send it into fits and freeze the program.
The first thing to keep in mind when working with FileMaker and ExecuteSQL() is the difference between tables and table occurrences. This is a concept that's somewhat unique to FileMaker. Succinctly, tables store the data, but table occurrences define the context of that data. Table occurrences are what you're seeing in FileMaker's relationship graph, and the ExecuteSQL() function needs to reference the table occurrences in its query.
I agree with MissJack regarding the need to start small in building the SQL statement and use the Data Viewer in FileMaker Pro Advanced, but there's one more recommendation I can offer, which is to use SeedCode's SQL Explorer. It does require the adding of table occurrences and fields to duplicate the naming in your existing solution, but this is pretty easy to do and the file they offer includes a wizard for building the SQL query.

Include spell checking in SQL 'LIKE'

I am using sql 'like' to search songs by genre. Like,
select * from songs where genre like %hip-hop%
and 'hip-hop' will be received by a search bar.
but if the user ACCIDENTLY writes 'jip-jop', that's not going to work for 'hip-hop'. I want it to work even then.
how do i make this happen?
you are looking for http://en.wikipedia.org/wiki/Levenshtein_distance
An SQL implementation is available here http://www.artfulsoftware.com/infotree/queries.php#552
There is also an SQLite function that does that. editdist3.
However, you must be careful, because the edit distance from hip-hop to jip-jop is 2, and the edit distance from pop to rap is also 2.

Search book by title, and author

I got a table with columns: author firstname, author lastname, and booktitle
Multiple users are inserting in the database, through an import, and I'd like to avoid duplicates.
So I'm trying to do something like this:
I have a record in the db:
First Name: "Isaac"
Last Name: "Assimov"
Title: "I, Robot"
If the user tries to add it again, it would be basically a non-split-text
(would not be split up into author firstname, author lastname, and booktitle)
So it would basically look like this:
"Isaac Asimov - I Robot"
or
"Asimov, Isaac - I Robot"
or
"I Robot by Isaac Asimov"
You see where I am getting at?
(I cannot force the user to split up all the books into into author firstname, author lastname, and booktitle, and I don't even like the idea to force the user, because it's not too user-friendly)
What is the best way (in SQL) to compare all this possible bookdata scenarios to what I have in the database, not to add the same book twice. I was thinking about a possibility of suggesting the user: "is THIS the book you are trying to add?" (imagine a list instead of the THIS word, just like on stackoverflow - ask question - Related Questions.
I was thinking about
soundex
and maybe even the
like
operators, but so far i didn't get the results i was hoping.
You can implement significantly better algorithms for fuzzy matching than soundex/difference, take a look at Beyond SoundEx - Functions for Fuzzy Searching in MS SQL Server.
You could also look at implementing a Full Text catalog and using the "search engine" style FREETEXT() which:
Is a predicate used in a WHERE clause
to search columns containing
character-based data types for values
that match the meaning and not just
the exact wording of the words in the
search condition
Depending on what your doing you could also perhaps use an ISBN web service to get normalized data.

MySQL: select the closest match?

I want to show the closest related item for a product. So say I am showing a product and the style number is SG-sfs35s. Is there a way to select whatever product's style number is closest to that?
Thanks.
EDIT: to answer your questions. Well I definitely want to keep the first 2 letters as that is the manufacturer code but as for the part after the first dash, just whatever matches closest. so for example SG-sfs35s would match SG-shs35s much more than SG-sht64s. I hope this makes sense whenever I do LIKE product_style_number it only pulls the exact match.
There normally isn't a simple way to match product codes that are roughly similar.
A more SQL friendly solution is to create a new table that maps each product to all the products it is similar to.
This table would either need to be maintained manually, or a more sophisticated script can be executed periodically to update it.
If your product codes follow a consistent pattern (all the letters are the same for similar products, with only the numbers changing), then you should be able to use a regular expression to match the similar items. There are docs on this here...
It sounds like what you want is levenshtein distance .
Unfortunately, there isn't a built-in levenshtein function for mysql, but some folks have come up with a user-defined function that does it(deadlink).
You will probably want to do it as a stored procedure, as I expect that the algorithm may not be trivial.
For example, you may split the term at the -, so you have two parts. You do a LIKE query on each part and use that to make a decision.
You could just loop though, replacing the last character with "%" until you get at least one result, in your stored procedure.
Sounds like you need something like Lucene, though i'm not sure if that would be overkill for your situation. But it certainly would be able to do text searches and return the ones most similar first.
If you need something more simple I would try to start by searching with the full product code, then if that doesn't work try to use wildcards/remove some characters until you return a result.
JD Isaacks.
This situation of yours is very simple to solve.
It`s not like you need to use Artificial Intelligence like the Google.
http://www.w3schools.com/sql/sql_wildcards.asp
Take a look at this manual at w3schools about wildcards to use with your SELECT code.
But also you will need to create a new table with 3 columns: LeftCode, RightCode and WildCard.
Example:
Rows on Table:
LeftCode = SG | RightCode = 35s | WildCard = SG-s_s35s
LeftCode = SG | RightCode = 64s | WildCard = SG-s_t64s
SQL Code
If the user typed the code that matches the row1 of the table:
SELECT * FROM PRODUCTS WHERE CODE LIKE "$WildCard";
Where $WildCard is the PHP variable containing the column 3 of the new table.
I hope I helped, even 4 years late...