MySql extract data from a database based on two columns - mysql

I Am trying to create/run an my sql query in such a way that the sql selects data based a some conditions from Column 1 (USER) but at the same time Excludes some data, based on some conditions from column 2 (ADDRESS)
E.g.:
SELECT ADDRESS,USER
FROM Data1.Table1
WHERE FIELD(USER,'%AMIT%','%JOHN%','%SANDEEP%','%WARNE%')
AND ORIGINATING_ADDRESS NOT LIKE 'MUMBAI','CHINA','PAKISTAN'
This is giving error.Can some one please help ?

Use NOT IN to discard list of values from select. Considering that you want to discard when there is exact match
ORIGINATING_ADDRESS NOT IN ('MUMBAI','CHINA','PAKISTAN')
When you want to use pattern search and discard the use this
ORIGINATING_ADDRESS NOT LIKE '%MUMBAI%' OR
ORIGINATING_ADDRESS NOT LIKE '%CHINA%' OR
ORIGINATING_ADDRESS NOT LIKE '%PAKISTAN%'

For a set of values, use NOT IN, instead of NOT LIKE.

You might find regular expressions simpler for this purpose:
SELECT ADDRESS,USER
FROM Data1.Table1
WHERE USER REGEXP 'AMIT|JOHN|SANDEEP|WARNE' AND
ORIGINATING_ADDRESS NOT REGEXP 'MUMBAI|CHINA|PAKISTAN';

Related

MySQL update order (row number) from comma delimited list - the simplest way

I have a table with two columns ID and ORDER, what I want to achieve is to update ORDER column as list numbers (like 1, 2 ...).
I have input as comma-delimited list of IDs:
21545,13117,21538,940,19658,21547,21532,7404,19663,19666,863,13114,13121,11769,13147,13156,972,13165,13174,13182,853,19671,7429,935,1015,931,986,996,991,953,893,920,899,906,20972,886,873,21574,21548
I need to update ORDER so 21545 = 1, 13117 = 2 and so on.
What is the simplest way to achieve this?
Maybe this is an easy task for you, but I am an MSSQL developer, so please don't mind me asking this. Thanks.
You can use the FIELD function to return the position of a value in a set of values if you are building the query from scratch e.g.
SELECT FIELD(13117, 21545,13117,21538,940,19658)
If you have a string which is comma separated, you can use FIND_IN_SET:
SELECT FIND_IN_SET(13117, '21545,13117,21538,940,19658')
In both cases the output is 2.
You can find a demo showing the use of these functions in an UPDATE query on dbfiddle

SQL - If input string is empty return all rows , otherwise try to match

I have table matches with these columns:
|sport|region|country|league
If the input string for sport is empty I want to return everything and don't bother with matching region, country etc.
If sport is not empty then find rows with matched sports and proceed to region and do the same thing.
Is this possible to do in SQL? I know I can filter this out in PHP and then run different SQL queries.
Try this
WHERE (sport_param IS NULL OR sport_column = sport_param)
You might want to use the LIKE operator or consider case-insensitive checking instead of simply comparing the exact sport.
Take a look at this
... you can basically make an if statement in SQL to match whether sport is empty or not and depending on that execute two different queries.

passing a variable to SQL statement in KNIME

Using KNIME, I would like to analyze data in a specific subset of columns in my database
but without using limiting SQL queries such as
Select *
From table
Where name like 'PAIN%'
Is there a way to do this in KNIME?
Try to find specific value within the column of choice by using:
Select distinct(column_name) from table;
You can pick from the expected result to filter your data
Select * from table column_name like 'result_one';
Assuming the column_name data type is in character.
To filer columns use the "Column Filter" node. You can filter the columns specifically, by RegEx on the column name or by column type (int, double, etc.) To filter rows based on content, use the "Row Filter" node, and select column to test and "filter based on collection elements" using pattern matching. This can also use RegEx. For mulitple columns use multiple nodes.
the knime did not support like for now, so I used the mysql locate or FIND_IN_SET function
SELECT id FROM address where LOCATE($street_Arr[0]$,street) > 0
SELECT id FROM address where FIND_IN_SET($street_Arr[0]$,street) > 0
however in the same situation u might be able to use knime joins much faster.

MySQL query to perform pattern matching

In MySQL how to search a value in a field that contains value separated by commas?(I have to search for (5,6) and it should return the field that contains all possibilities {5,6,(5,6)}
Is it possible?
You can try this
MyModel.where("field LIKE (?)", "%5,6%")
MyModel.where("filed REGEXP '5,6'")
There are many methods in MySQL for pattern matching, such as like ,RLIKE, MATCH-AGAINST,FIND_IN_SET, etc. The exact one depends on your need. Explain your sample data and desired result; then I can say which function will best help you.

MySQL UNION query correct handling for 3 or more words

I've to ask your help to solve this problem.
My website has a search field, let's say user writes in "Korg X 50"
In my database in table "products" i have a filed "name" that holds "X50" and a field "brand" that hold "Korg". Is there a way to use the UNION option to get the correct record ?
And if the user enters "Korg X-50" ?
Thank you very much !
Matteo
May be you should use full-text search
SELECT brand, name, MATCH (brand,name) AGAINST ('Korg X 50') AS score
FROM products WHERE MATCH (brand,name) AGAINST ('Korg X 50')
As far as I understand you don't need UNION but something like
SELECT * FROM table1
WHERE CONCAT(field1, field2) LIKE '%your_string%'
On client side you get rid of all characters (like space, hyphen, etc) in your_string that appears in user input and cannot be in field1 or field2.
So, user input Korg X 50 as well as Korg X-50 becomes KorgX50.
you will need to get some form of searchable text.
either parse out the input for multiple key words and match each separately, or perhaps try to append them all together and match to the columns appended in the same way.
you will also need either a regex, or maybe a simpler search and replace to get rid of spaces and dashes after the append before the comparison.
in general, allowing users to search for open ended text strings is more complicated than 'what union do i use'... you will ideally also be worried about slight misspellings and capitalization, and keyword order.
you may consider pulling all keywords out from your normal record into a separate keyword list associated with each product, then use that list to perform your searches.
If you do not want to parse user input and use as it is, then you will need to use a query like this
select * from products where concat_ws(' ',brand,name) = user_input -- or
select * from products where concat_ws(' ',brand,name) like %user_input%
However, this query won't return result if user enters name "Korg X-50" and your table contains "Korg" and "X50", then you need to do some other thing to achive this. You may look at http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_soundex however it won't be a complete solution. Look for text indexing libraries for that ex: lucene