mysql select query (dictionary search) - mysql

I have a table and I want to get the row with the value that most closely resembles the searched text. For example if searched text is 'rakul' it should be able to select row with value 'rahul'

Just change the Customers value to your table name and the FirstName to what column you want to search and the David to your word want to search for it
SELECT * FROM Customers WHERE FirstName LIKE '%David%';

Related

What is the new table name after joining two tables in sql?

I read the previous posts but I couldn't find one that answered my question.
What would be the name of the table that is made by joining two tables? The reason why I need the name is because I would like to change the column name of the new table using the ALTER TABLE (Table name) RENAME COLUMN (A) to (B). If there is no specified name, how can I name the new table?
ex
SELECT
client_id
,last_name
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name
FROM BUSINESS_CLIENT;
I would like to rename the column to last_name/bus_name instead of last_name
In the case of a query what temporal table a query might create internally is not relevant, because you a making a query and getting data back, it doesn't stay in the database as a table, there is no such table. Unless we make it.
if You want TSQL to change a column name it would affect your union query and I base my answer on Your
'I would like to rename the column to last_name/bus_name instead of last_name'
And think this is what you're looking for. Please correct me if it isn't.
In generic SQL what we're doing is putting a label on both projections that are to be displayed in the same column
SELECT
client_id
,last_name [last_name/ bus_name]
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name [last_name/ bus_name]
FROM BUSINESS_CLIENT;
update, in MySQL notation uses AS and quotes instead of angle brackets
SELECT
client_id
,last_name as "last_name/ bus_name"
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name as "last_name/ bus_name"
FROM BUSINESS_CLIENT;

mysql search 3 columns with dynamic count of words

I have a table with 3 fields. These fields are: name, surname, address. When I search by one word and any of fields contains the word, this row should be selected. When I search by 2 words 2 of 3 fields should have these words and so on. I have ready query in my hand.
This is for 2 word search string:
where (name like %word1% or surname like %word1% or address like %word1%)
and (name like %word2% or surname like %word2% or address like %word2%)
Number of intermediate ands = words - 1. Where part is generated depending on number of words in search string. How can I optimize this query?

How to find MySQL records having one field's content within any row of another field

The problem is simple. Is there a MySQL query allowing, in a single table, to find all records having some content within any row of another column ?
In other words, I have a table with 2 columns : "FirstName" and "LastName".
My automatic filler has melted some of them.
So I would like to find all occurences of "LastName"s that also appear in column "FirstName".
That way, all the Doe JOHN would be detected, because "John" must 100% be in the list of "FirstName"s, whereas John DOE would not be detected, because there is nearly no chance to find DOE as a first name...
Just perform a self join over the table. It should look something like this:
SELECT
*
FROM
CUSTOMER C1
INNER JOIN
CUSTOMER C2 ON (C1.FirstName = C2.LastName OR C1.LastName = C2.FirstName);
I'm guessing you want something like:
SELECT *
FROM nametable
WHERE firstname IN (select lastname FROM nametable)
That will find row where the first name exists as a last name in the table.

Searching in SQL server and returning records that match both search criteria but also comparison with other records

I will try to explain you in a simple way what I mean in the title. Let's say that I have a table called [Persons] and each record in it is a person, having as columns:
Name, Surname and HashId.
Now, in my code I am able to search in this table by using as criteria Name and Surname, like for example:
SELECT *
FROM [Persons]
where Name = '..' AND Surname = '..'
This will return ALL the records that match the searching criteria. The 3rd column is a computed column that depends on the Name and Surname. So if I search for example someone called "Alex Foo" and in the database there are 2 people with the same name and surname, their HashID will be the same. What I want now is that my query doesn't return those 2 records but only one.. the query has to be able to check whether or not the HashID's are the same and in that case return only 1 record for all those that satisfy this rule. How can my modify my SQL query to reach this purpose? Thanks in advance!
EDIT: I omitted an information that now comes clear with your answers. I must filter over the HashId for the following reason: If there are 2 records with the name "Alex" and "alex", their HashId will be the same according to my logic, which is the correct logic for my purposes (the whole code behind is more complex than the explanation I gave you). But a simple select over the name "alex" would return the 2 different records with respectively name "Alex" and "alex". That's why the HashId is important.
Can't you just group by the name,Surname,HashId?
Like this:
SELECT name,Surname,HashId
FROM [Persons]
where Name = '..' AND Surname = '..'
GROUP BY name,Surname,HashId
If all there columns are the same then it will return one row for this
Edit
As Panagiotis Kanavos pointed out this will be the same as doing a distinct on the columns too. Maybe this is cleaner. Like this:
SELECT DISTINCT name,Surname,HashId
FROM [Persons]
where Name = '..' AND Surname = '..'

Matching Different Value within One Row

I want to perform a query whereby I want to check whether on the columns A has either certain values. A could have only X , X and Y or a combination of X Y and Z.
To give a better understanding. I am checking a book's author within a table itself. The table has the BOOK_ID , BOOK_TITLE , AUTHOR_NAME, AUTHOR_ORDER.
So a book might have 1,2 or 3 authors, listed in order written inside the AUTHOR_ORDER row. I am trying very hard to reach an output where if a book has 3 authors, it will display accordingly from the first author to the third author. I am now stuck in the part where I need to compare the value and present it in the output.
Any idea how to achieve this in MYSQL output?
Sample :
The output result is more or less like this:
If the title has au_ord of 1,2 and 3, there shall be a new column with all the authors name listed in ascending.
So for example, for title BU1032, the Author row will be Bennet, Green
I think GROUP_CONCAT is what you are after:
SELECT Title_ID,
Title,
GROUP_CONCAT(au_LName ORDER BY au_Ord) AS Authors
FROM Books
GROUP BY Title_ID, Title;
SQL FIDDLE