I have a table that holds user data. It looks something like this:
ID Name Surname Age
1 Alice Moore 23
2 David Moore 45
If working with only one field, lets say Name, which I have to use for my search key then I could simply do something like this:
Select * from Users where Name Like '% Al %'
ORDER BY DIFFERENCE(Name , 'Al') desc;
and get the data for user 1.
If I want the search operation to take into account other fields too, lets say Surname, I am not sure how to proceed.
How would order by know what column to use in the end so I can return the list based on relevance.
Before anything I'd like to say that I'm not sure how advisable it would be following my solution below in tables with a considerable amount of data. It's simply something I came up that I think should work for your case.
First try
What you could do is order by the minimum difference of all columns of interest in each row.
SELECT FirstName,
LastName,
(SELECT MIN(v)
FROM (VALUES (DIFFERENCE(FirstName , 'Al')),
(DIFFERENCE(LastName , 'Al'))) AS value(v)) as diff
FROM Users WHERE FirstName LIKE 'Al%' OR FirstName LIKE 'Al%'
ORDER BY diff DESC;
Try an interactive fiddle here.
Second try
Another option is ordering your result in a descending order of the difference of the concatenated string of all the rows of interest.
SELECT FirstName,
LastName
FROM Users
WHERE CONCAT (FirstName, LastName) LIKE 'Al%'
ORDER BY
DIFFERENCE(CONCAT (FirstName, LastName) , 'Al') DESC;
Try an interactive fiddle here.
Related
I just started my database and query class on Monday. We met on Monday and just went over the syllabus, and on Wednesday the network at school was down so we couldn't even do the power point lecture. Right now I am working on my first homework assignment and I am almost finished but I am having trouble on one question.
Here is is...
Write a SELECT statement that returns one column from the Customers table named FullName that joins the LastName and FirstName columns.
Format the columns with the last name, a comma, a space, and the first name like this:
Doe, John
Sort the result set by last name in ascending sequence.
Return only the contacts whose last name begins with letters from M to Z.
Here is what I have so far...
USE md0577283
SELECT FirstName,LastName
FROM Customers
ORDER BY LastName,FirstName
My question is how do I format is Lastname, FirstName like the professor wants and how do I only select names M-Z?
If someone could point me in the right direction I would greatly appreciate it.
Thank you.
PS With all do respect, I didn't ask for the answer I asked for a nudge in the right direction so why the down vote guys?
USE md0577283
SELECT LastName + ', ' + FirstName FullName
FROM Customers
WHERE LastName LIKE '[M-Z]%'
ORDER BY LastName,FirstName
You want to add two things: create an expression to return the name in the requested format
(LastName + ", " + FirstName as Name)
USe a "where clause" to filter what is returned: where LastName >= "M" and LastName <= "Z" perhaps.
Simply write like this.
If you want to get names from m to z.
SELECT LastName, FirstName
FROM Customers
WHERE FirstName between 'M%' and 'Z%'
ORDER BY LastName, FirstName
If you want to get names from m and z only.
SELECT LastName, FirstName
FROM Customers
WHERE FirstName LIKE 'M%' OR FirstName LIKE 'Z%'
ORDER BY LastName, FirstName
My Products table has almost 10,000 records where i want the "artist" ( varchar ) column name to sort by Firstname and Lastname. In the where clause i am sending initial letter of either first name or lastname. This Query works for few records but not for all, its probably because of the pattern of the records ?
Here is my query
SELECT * FROM Products WHERE SUBSTRING(artist, 1, 1) Like 'C'
Doesnt return the records... the way i want it ...Few of the records of the table ( artist) column are below
Comtesse Mathilde duMonceau de Bergendael
Mulholland, S.J.
Cleminson
NULL
Samuel Jackson
Your LIKE clause is wrong. Also you should not be using LIKE at all here, as you're only filtering by one letter:
.... WHERE LEFT(artist, 1) = 'C'
EDIT LEFT is actually faster than SUBSTRING.
Also, you can not expect anything that's not starting with a C to be returned, as you're explicitly only asking for entries that start with a C.
I guess what you really need is the following:
.... WHERE LEFT(FirstName, 1) = 'C' OR LEFT(LastName, 1) = 'C' ORDER BY LastName, Firstname
If you only have the Artist column, things get complicated, as you actually do need to use LIKE, which makes things pretty slow
.... WHERE Artist LIKE `%C%`
returns any artist, the name of which contains a C anywhere in the name. This would also return these:
Clemens Test
Test Cutler
Arachnophobia
User this where clause
where left(artist, 1) = 'C'
or
where artist like 'C%'
I have a data table with health premium rates that depend on age, gender, and zip code. I want to create a query that gives me just the lowest price premium by a particular plan name/gender/zipcode combo.
Example, what's the lowest price rate?
Zip Gender Age Lowest_price_rate
======================================================
10001 F 90 ?
90210 M 65 ?
I'm assuming your're using some form of SQL and that you have the rates in the table already.
Group the entries by whatever you want and select the minimum one. The query might look something like this:
SELECT Zip,Gender,Age,MIN(rate) AS Lowest_price_rate FROM MyTable GROUP BY Gender,Age,Zip
Of course, you can put a standard WHERE clause in right after the FROM MyTable part if you want to look at only certain criteria. That query is untested, but it should work.
SELECT MIN(Lowest_price_rate) FROM TABLE_NAME WHERE plan_name ='' AND gender = '' AND Age = ''
Use the above query with the necessary values. (Tablename and values for the fields)
I want to fetch all the records of First_Name, LastName, First Name Last Name in a mysql Query.
For example,
mytable looks like this:
rec Id First Name Last Name
1 Gnaniyar Zubair
2 Frankyn Albert
3 John Mathew
4 Suhail Ahmed
Output should be like this:
Gnaniyar Zubair, Frankyn Albert, John Mathew, Suhail Ahmed
Give me the SQL.
If this must the done in the query, you can use GROUP_CONCAT, but unless you're grouping by something it's a pretty silly query and the concatenation should really be done on the client.
SELECT GROUP_CONCAT(FirstName + ' ' + LastName
ORDER BY FirstName, LastName
SEPARATOR ', ') AS Names
FROM People;
It is not a matter of getting one row with all the records, but a matter of representation of data. Therefore, I suggest to take a simple SELECT query, take the records you need, then arrange them in the view layer as you like.
On the other hand, why do you need to solve this record concatenation at SQL level and not on view level?
If you wanted to get them in just one row, you're probably not using your database properly.
If you just want to join together the first and last names, that's easy:
SELECT CONCAT(`First Name`, ' ', `Last Name`) FROM mytable
I wish to find all rows in a table where one column is a substring of another column.
In other words, suppose I have a table (called people) with two columns: firstname and lastname, and I want to find all people like "rob robinowitz" and "jill bajillion".
Is there a way to do something like "select * from people where lastname like %firstname%"? (But something which actually works).
You were close
select * from people where lastname like '%' + firstname + '%'
Alternative way (may be even faster)
select * from people where charindex(firstname,lastname)>0
If you are using MySQL you could
SELECT * FROM people WHERE INSTR(lastname, firstname) <> 0