Front-end SQL Search - Dynamically check similar values? - mysql

I have a front-end DB search on my website: https://ygoprodeck.com
Currently it checks card names. A user can type in "magician" and get loads of results.
But if a user mis-spells it and types "magican" then no results show.
I've been tracking user inputs and it seems a lot of users are mis-spelling card names and getting no results.
A common example: There are cards with D/D/D in the name but users often type DDD which means they get no results.
Is there a pure SQL method to modify this behaviour or would it need to be implemented through JS/PHP?
Thanks!

How about saving all the mis-spells in new table like
CREATE TABLE misSpells(
misSpell VARCAHR,
correspondingOne VARCHAR
)
Examples:
magician, magician
magican, magician
DDD, DDD
D/D/D, DDD
and query this table for the corresponding one?

Related

How do I insert data from a reference table to the corresponding field

I'm sorry if my question sounds confusing.I just started learning web2py recently,in this exercise I'm trying to make a simple users management webpage with the admin can assign the users theirs work lists,note and deadline
db.define_table('auth_manager',Field('name','string',requires=IS_NOT_EMPTY()))
db.define_table('manager',Field('user','string','reference user.name'),
Field('workname','text',requires=IS_NOT_EMPTY()),
Field('deadline','date'),)
db.manager.deadline.requires=IS_DATE_IN_RANGE(format=T('%Y-%m-%d'),
minimum=now,maximum=now+datetime.timedelta(60))
I thought of adding the manager's username in auth_manager table using appadmin's new record function.This is my user table
db.define_table('user',Field('name','string',requires=IS_NOT_EMPTY()),
Field('password','password'),
Field('workname','text'),
Field('deadline','date'),
format='%(name)s')
I wanted to insert workname and deadline into user table right after I add those form on manager but I couldn't find any other methods except the update or update_or_insert functions but both don't work because those fields can't be empty and their ids aren't the same value and multiple references to a single table don't work .
One last question,I want to use web2py's RBAC but the first & last name fields are often unnecessary if I want to use a full name field is there other way to do it?
Sorry for the long post,I hope I made my question clear.
You can use the tables from auth and let web2py to handle everything in between.
The following code should resolve your problem:
db.define_table('manager', 'reference auth_user'),
Field('workname', 'text', requires=IS_NOT_EMPTY()),
Field('deadline', 'date'))

Database schema practical approach

I want to model 2 entities in database: CafeBrand and Cafe. I have pretty much the same properties in both entities:
CafeBrand{
foodDescription,
website,
email,
phone
}
cafe{
foodDescription,
website,
email,
phone
}
So let's say in case of McDonalds all 'cafes' would have the same foodDescription: 'Junk food'. But some other brands might have separate food description for separate cafes('sandwiches', 'drinks only', ...).
Same with website/email/phone properties: cafe might have its own website/email/phone but also it could be using the same website/email/phone for all of them. Quite often the same CafeBrand has one website but different email/phone for its different cafes.
My question is: is it wise to store these properties as it is and then use if/else (in SQL or code) to get a proper description,website,email,phone (if cafe.website == null then use cafebrand.website) or is it better to use relationships to separate tables 'FoodDescription', 'Website' The data won't be written to the database very often and most of the time only select statements will be used.
And if Company has a single cafe. How should this foodDescription/Website be split into CafeBrand/Cafe tables.
As Bill Gregg has mentioned you should probably put all similar data in one table. So you'll receive following structure:
cafe {
foodDescription,
website,
email,
phone,
brand
}
Because of foodDescription, website and so on columns will contains pretty unique values I assume, you won't gain any profit with separation the data into different tables.

Creating a view in SQL - duplicate fields error

Hey there I am trying to learn SQL by trying to complete online questions and I'm trying to create a view.
I have created a SELECT query that works and now trying to turn it into a view, though when following the online instructions and enter the script that should create a view I come up with the duplicate field error.
I've looked into it and it may be "doctor.fullname" as a duplicate field but this is from another table. Andway I have deleted and still tried it but the error still appears.
I know there is probably an easy solution and sorry to bother you with this but it will be must appreciated.
CREATE VIEW patient_registration_form
AS
SELECT
patient.patient_id,
patient.nok_no,
patient.f_name,
patient.s_name,
patient.sex,
patient.dob,
patient.marital_status,
patient.date_registered,
nok.tel_no,
nok.full_name,
nok.address,
nok.relationship,
doctor.doctor_id,
doctor.clinic_no,
doctor.full_name,
doctor.address
FROM doctor, patient, nok
WHERE doctor.doctor_id = patient.doctor_id
AND nok.nok_no = patient.nok_no;
You're returning several columns with the same name... full_name and address... even though they're from different tables. In this case, you have to give them different aliases:
SELECT
patient.patient_id,
patient.nok_no,
patient.f_name,
patient.s_name,
patient.sex,
patient.dob,
patient.marital_status,
patient.date_registered,
nok.tel_no,
nok.full_name as nok_full_name,
nok.address as nok_address,
nok.relationship,
doctor.doctor_id,
doctor.clinic_no,
doctor.full_name as doctor_full_name,
doctor.address as doctor_address
You have two full_name listed... one for nok.full_name and one for doctor.full_name. One needs to be renamed such as nok.full_name as nok_full_name.

Text search in database column

im using vb 2010 express edition. I have a database (Sql) and a table "students" in the database. It has a data like this:
StudentId Name Surname Classs
2266 Mike Brown 8
2773 Carol Smith 6
2883 Michel Old 7
2773 Miray Edem 6
27736 Cindy Temiz 7
......................................
......................................
there are lots of students. I want to put a search textbox on my form. User will search a student by name. When user presses a key on search textbox, for example "M" a box will appear and shows students which contains "M". (Mike, Michel, Miray) . It will work like google search. is there any way for me to help for this... Please share your ideas...
You should create a customautocomplete class for your textbox and set its autocomplete
Something like:
Dim tbox As New TextBox
Dim aCol As New AutoCompleteStringCollection
For Each student As String In dt.results("students")
aCol.Add()
Next
tbox.AutoCompleteSource = AutoCompleteSource.CustomSource
tbox.AutoCompleteCustomSource = aCol
tbox.AutoCompleteMode = AutoCompleteMode.SuggestAppend
I prefer VB.NET so I have provided an example in VB.NET as no language was specified.
I always use JQuery's autocomplete: http://docs.jquery.com/Plugins/autocomplete
Build your backend to do the search based on an input string and then have your ui do a json call with the autocomplete to return the results.
Is this a SQL query question or a software question, when you are asking for help? This sounds like a class assignment.
How are you going to be connecting to and querying your database? Are you going to write your query in a stored procedure in the database, or are you going to bind your software objects to the database tables?
Is it a convention at your location to use one kind of data access or query over another?

DynamicQuery: How to select a column with linq query that takes parameters

We want to set up a directory of all the organizations working with us. They are incredibly diverse (government, embassy, private companies, and organizations depending on them ). So, I've resolved to create 2 tables. Table 1 will treat all the organizations equally, i.e. it'll collect all the basic information (name, address, phone number, etc.). Table 2 will establish the hierarchy among all the organizations. For instance, Program for illiterate adults depends on the National Institute for Social Security which depends on the Labor Ministry.
In the Hierarchy table, each column represents a level. So, for the example above, (i)Labor Ministry - Level1(column1), (ii)National Institute for Social Security - Level2(column2), (iii)Program for illiterate adults - Level3(column3).
To attach an organization to an hierarchy, the user needs to go level by level(i.e. column by column). So, there will be at least 3 situations:
If an adequate hierarchy exists for an organization(for instance, level1: US Embassy), that organization can be added (For instance, level2: USAID).--> US Embassy/USAID, and so on.
How about if one or more levels are missing? - then they need to be added
How about if the hierarchy need to be modified? -- not every thing need to be modified.
I do not have any choice but working by level (i.e. column by column). I does not make sense to have all the levels in one form as the user need to navigate hierarchies to find the right one to attach an organization.
Let's say, I have those queries in my repository (just that you get the idea).
Query1
var orgHierarchy = (from orgH in db.Hierarchy
select orgH.Level1).FirstOrDefault;
Query2
var orgHierarchy = (from orgH in db.Hierarchy
select orgH.Level2).FirstOrDefault;
Query3, Query4, etc.
The above queries are the same except for the property queried (level1, level2, level3, etc.)
Question: Is there a general way of writing the above queries in one? So that the user can track an hierarchy level by level to attach an organization.
In other words, not knowing in advance which column to query, I still need to be able to do so depending on some conditions. For instance, an organization X depends on Y. Knowing that Y is somewhere on the 3rd level, I'll go to the 4th level, linking X to Y.
I need to select (not manually) a column with only one query that takes parameters.
=======================
EDIT
As I just said to #Mark Byers, all I want is just to be able to query a column not knowing in advance which one. Check this out:
How about this
Public Hierarchy GetHierarchy(string name)
{
var myHierarchy = from hierarc in db.Hierarchy
where (hierarc.Level1 == name)
select hierarc;
retuen myHierarchy;
}
Above, the query depends on name which is a variable. It mighbe Planning Ministry, Embassy, Local Phone, etc.
Can I write the same query, but this time instead of looking to much a value in the DB, I impose my query to select a particular column.
var myVar = from orgH in db.Hierarchy
where (orgH.Level1 == "Government")
select orgH.where(level == myVariable);
return myVar;
I don't pretend that select orgH.where(level == myVariable) is even close to be valid. But that is what I want: to be able to select a column depending on a variable (i.e. the value is not known in advance like with name).
Thanks for helping
How about using DynamicQueryable?
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
Your database is not normalized so you should start by changing the heirarchy table to, for example:
OrganizationId Parent
1 NULL
2 1
3 1
4 3
To query this you might need to use recursive queries. This is difficult (but not impossible) using LINQ, so you might instead prefer to create a parameterized stored procedure using a recursive CTE and put the query there.