I have a Table say tblName having fields and values
| ID | Name |
| 1 | Technical University |
| 2 | XYZ Lab Ltd. |
Now I have to match Name column with the following input values :
1- tech univ
2- tech universities
3- xyz Labs
I am not able to write query to get result from the table.
If I match Name column with the input value tech univ or tech universities then query should response result set Technical University from the tblName.
Please suggest.
Thanks in advanced.
Well you can do one thing map name to different names such as
ID I_ID NAME
1 1 tech university
2 1 tech uni
3 1 tech univs
So this table will be link for your search queries I_ID is a foreign key which refers ID in primary table. I guess this could solve your issue and you can define several names for a single name.
Moreover use regular expression for more efficiency and you have to develop a custom function for that though the genral usage of REGEXP is as follows
SELECT id, name FROM tblName WHERE name REGEXP 'tech'
Please refer http://www.tutorialspoint.com/mysql/mysql-regexps.htm for more on REGEXP
For simple cases when you need to match begining of string you can use
select * from table where field LIKE 'Hello %'
The % sign is a wildcard.
But for the example you need I would rekomend to look on some full text indexing solutions that you put around mysql.
In MS SQL we use a Levenstein Distance function and you could build your own and extend mysql with it.
Here is some examples but I do not know if it will work
https://github.com/jmcejuela/Levenshtein-MySQL-UDF
https://github.com/jmcejuela/Levenshtein-MySQL-UDF/blob/master/levenshtein.c
Found one that should be pure SQL
http://openquery.com.au/blog/levenshtein-mysql-stored-function
test this it may help
SELECT * FROM tblNAME WHERE Name LIKE "%name%";
Related
I have a table with lots of features, but I used features as just one column.
tbl_shop
id | name | feature
I used php implode to save the data to feature, and it will show like this
id | name | feature
1 | shop_1 | wifi,smarttv,cr
When I use LIKE to search for the data I can actually get the shop properly if I just search for one feature like wifi
the thing is if i tried to search for wifi,smarttv or smarttv,cr there is no problem, but when I tried to search wifi,cr that time it would not find the shop, is there any way of searching data like this in MySQL ?
You can insert a % in between your shops (or partial shops)
select * from table where feature like '%wifi%cr%';
I have the following schema:
Contact:
ID| NAME |Contact
----------------------
1 | A |1234,4567
-----------------------
2 | B |2345,5678,9012
Here, as you can see Contact is a Multivalued field and A person can have as many as contact numbers.
Now, I want to count the number of contacts for each person.
How can I do that?
Note: I don't want to normalize this table.
Any help will be appreciable.
While I don't advise doing this, this is how you would do it:
SELECT ID, (LENGTH(Contact) - LENGTH(REPLACE(Contact,",","")) + 1) AS NumberOfContacts
FROM MyTable
The output will look like this:
ID| NumberOfContacts
1 | 2
2 | 3
The difference in length after the replace will tell you how many commas were in the string, then you would need to add 1 to include the last number. You should really structure the data properly, but if that is out of the question then you will need to work with something like this
So I have this list of 230 countries already in my database table called "countrylist" inside the "country" row. besides the "country" row, I also have the "code" row, which has the country code of that country in it.
The countries are in English and since I need the country names in 3 other languages as well, I duplicated the table for the 3 other languages too. I also programmed the PHP code to choose the right table based on the language.
I have the list of the country names in the other 3 languages ready and they have the same ordering.
BUT... this leaves me with 230 countries to replace in 3 languages, and that's a lot of wasted time for something which I already know should be much simpler than basic copy pasting. Although I don't even have a slight clue of how or if this can be done...
I want to know if there is a way to preserve the "code" row in the tables and replace all of the countries in the "country" row with a query/command?
Thanks, any help is much appreciated
INSERT INTO countries_fr (code) SELECT countries_en.code FROM countries_en
Assuming you have the tables countries_fr and countries_en, and both have the code column.
This will insert all the code from the countries_en table into the code of countries_fr table.
You create a relational model.
country (id, name)
countryLang (countryId, lang, translatedName)
This way, the languages are independent of your parent country name.
| ID | NAME | COUNTRYID | LANG | TRANSLATEDNAME |
---------------------------------------------------
| 1 | Poland | 1 | en | Poland |
| 1 | Poland | 1 | pl | Polska |
See a demo
So it's normalized, you have to add columns for the other 3 names in the table, so (replace the languages with those desired):
alter table countries add french varchar(45),spanish varchar(45),russian varchar(45);
Since the code is exactly the same you can use it like a key field this one time.
update countries c set c.french=(select country from french where french.code = countries.code),
c.spanish=(select country from spanish where spanish.code = countries.code),
c.russian=(select country from russian where russian.code = countries.code);
On the other hand...
If you were to simply replace the country name you would have to maintain 4 sets of code.
update countries set country=(select country from language2 where language2.code=countries.code);
None of the answers were practical due to the nature of the code from before, so I had to redo everything manually with Dreamweaver copy and replace. Took an hour but it was done right.
i will try to explain the problem.
I have this structure:
offers
--------------
id_offer|offer|company
1 | web programmer| Microsoft
2 | web programmer| Microsoft
tags
--------------
id_tags | tags
1 | php
2 | js
3 | asp
offers_has_tags
---------------
offers_id_offer (fk) | tags_id_tags (fk)
1 | 1
1 | 2
1 | 3
2 | 1
2 | 2
If i use a system like Stackoverflow, where each title of question is unique, there is no problem with the code below. But if i can have various offers with same title, and with same owner, i can't do WHERE offers = ?,
So, i need a different approach to select a specific job. Can't be the title, and can't be the owner+title, because the same owner can have various offers with same title.
INSERT INTO `offers_has_tags` (
offers_id_offer,
tags_id_tags
)
VALUES (
(SELECT id_offer FROM offers WHERE offer = ?), //here
(SELECT id_tags FROM tags WHERE tags = ?))
How can i select an offer when exists more than one, with same title and same owner ?
Simple answer: there is no way to retrieve exactly one row from table if your where clause is not filtering rows by PK columns.
It is not 100% clear what You are trying to achieve. However, primary key is used to uniquely identify the row. This means that in this case You should use offer_id in where clause (in your insert statement in original question).
I guess that you have some UI in front of this - why don't your UI send offer_id to data access code instead of offer name?
But, if You want to insert all offers with same name and owner to offer_has_tag, try this (it is T-SQL syntax but as far as I can recall it should work on MySQL also):
INSERT INTO `offers_has_tags` (
offers_id_offer,
tags_id_tags
)
SELECT id_offer, (SELECT id_tags FROM tags WHERE tags = ?)
FROM offers WHERE offer = ? AND company = ?)
Please note that You should use id_tags instead of tag name in your queries. Only use descriptive attributes in filtering the list for end user. Otherwise, use primary key columns.
Could you be a bit more specific? In which situation are you executing the sql, for which purpose and in which way are the arguments given? Maybe post the code that's in front of these statements.
That could helping answer your question. The thing I'm wondering at the moment, why is it a problem to get more than one case as result?
I have a table containing data about some users.
Many of them use our in house email system at OLDHOST.com. We have updated to a newer system at NEWHOST.com. All the users usernames are the same, so if you had dave#OLDHOST.com, you are now dave#NEWHOST.com
Is there a better way to change all the email fields in the users table without selecting all the rows in say PHP, then checking if the email has OLDHOST in it, then replacing the string to NEWHOST?
Is there a baked in awesome SQL statement to help with this?
Example of some of the table (simplified)
id | firstname | surname | email
------------------------------------------------
1 | dave | smith | a21dsmith#OLDHOST.com
2 | barry | jones | a21bjones#OLDHOST.com
etc.
All that needs to be changed is any emails that contain OLDHOST (Not all do) to NEWHOST.
You'll need to replace the relevant part of each string within an update statement, grabbing the hostname substring after the # with a REPLACE, and replacing it.
UPDATE table SET email=REPLACE(email,'OLDHOST.com', 'newhost.com');
Note: REPLACE() is case-sensitive, so you can use LOWER(email) inside the REPLACE function if you need to catch all case possibilities, as below:
UPDATE table SET email=REPLACE(LOWER(email),'oldhost.com', 'newhost.com');
This will also convert all your email addresses to lowercase, so be aware of that :)