SELECT DISTINCT example_type FROM Table_name WHERE 80 IN (example_type);
The values I'm using are 66 and 80.
The values for the column are sometimes "66,80" and sometimes "80,66".
If it is a search for 80, it will only return a match if the column is "80,66" (or "80").
If it is a search for 66, it will only return a match if the column is "66,80" (or "66").
The column type is set to TEXT.
The following works as expected: (both are a match)
....WHERE 80 IN (66,80)...
....WHERE 80 IN (80,66)...
I'd like to keep the contents of the column as CSV.
What do I need to do to the MySQL query to get it work no matter the order of the CSV?
May be, you can use the following where clause:
WHERE column_name like '%80%'
If your two queries are right, then you can use union clause in mysql
SELECT ... FROM ... WHERE (your first condition)
UNION
SELECT ... FROM ... WHERE (your second condition)
SELECT DISTINCT example_type FROM Table_name WHERE FIND_IN_SET(80, example_type);
This is based on the final code:
WHERE ...a.is_special = 1
AND (a.foo_list = '' OR FIND_IN_SET($foo_id, a.foo_list))
AND (a.bar_list = '' OR FIND_IN_SET($bar_id, a.bar_list))
AND (a.baz_list = '' OR FIND_IN_SET($baz_id, a.baz_list))";
Related
I have a varchar(30) column that looks like this:
953-41
975-12
952-13
934-34
All numbers of the column share the structure of: 3 numbers and a dash followed by more numbers.
I want to make a query that works like SELECT * FROM tablename WHERE value = 95341
And get '953-41' only using numbers in the WHERE clause.
I can't change my database and remove the dash, I need to search with a numeric value on rows that mix the numbers I want with a dash in between.
you can try:
MYSQL:
SELECT * FROM tablename WHERE value = INSERT(95341,4,0,'-')
SQL Server:
SELECT * FROM tablename WHERE value = STUFF(95341,4,0,'-')
You can use this:
SELECT *
FROM yourTable
WHERE CAST(REPLACE(colName, '-', '') AS UNSIGNED) = 95341
I have Two tables in mysql database and I want to compare two columns each of them in a different table first table name "oc_product_option_value" has column:
product_option_value_id
20
21
22
23
50
100
and second table "oc_cart" has cuolomn
option
{"20":"228","24":"229"}
I want compare two table and select data from first table where "product_option_value_id" in second table.
I tried:
SELECT * FROM oc_product_option_value
WHERE product_option_id IN
(SELECT REPLACE(REPLACE(REPLACE(REPLACE(OPTION,'{',''),'}',''),':',','),'"','')
as `option` FROM `oc_cart`)
and no result
* columns Structure
"product_option_value_id" is int
"option" is TEXT
Heum.. Not sure that it will do what you expect but I think it's a first step:
1/ Returns only rows wich have a matching value in second table (oc_cart)
SELECT *
FROM oc_product_option_value acpoa
JOIN oc_cart acc ON acc.option REGEXP concat('"', acpoa.product_option_value_id, '"');
Be careful about naming a column with a reserved MySQL word (option)
EDIT :
2/ If you want to display a "result" (final_kept_column) after this comparison in order to display "value_id" or "option" even if there's no matching value in oc_cart, you can try someting like this :
SELECT *,
CASE WHEN acc.option IS NULL
THEN acpoa.product_option_value_id
ELSE 0
END AS final_kept_column
FROM oc_product_option_value acpoa
LEFT JOIN oc_cart acc ON acc.option REGEXP concat('"', acpoa.product_option_value_id, '"');
Hope this help
I have two select statements which are as mentioned below
SELECT 'MY OUTPUT'
SELECT * FROM MY TABLE
On execution MY OUTPUT is printed first and then there is a gap for next select.
I want to use something like UNION to combine two statements.
I'm using:
SELECT 'MY OUTPUT' UNION
SELECT * FROM MY TABLE
But, I am getting error:
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
SELECT 'MY OUTPUT' # this query return one column
UNION
SELECT * FROM MY TABLE # this query return more than one column
number of columns must be the same
SELECT 'MY OUTPUT' UNION # return one column
SELECT column1 FROM MY TABLE # return one column now it will work
but i think you want to do this no?
SELECT 'MY OUTPUT',column1,Column2,column3 FROM MY TABLE
So here's what's happening:
The error you're getting (All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.) is because there are more columns in one select than the other.
You could use the same blank columns in your first query as there are in [My Table] which would make your query look like:
SELECT 'MY OUTPUT' , '', '' ,'', '' --(no. of columns should match those in MY TABLE)
UNION
SELECT * FROM [MY TABLE]
I'm guessing you want an excel style cell merge which is not possible as the output of a select query unfortunately.
I have the following problem with a MySQL query in C#:
Given a list of strings, I want to query the database for any rows that match said strings. The strings are unique in that each string matches no more than one row. Today, my query looks something like this:
SELECT Id FROM SomeTable
WHERE SomeColumn IN("foo", "bar", "baz")
Now, ideally I would like to be able to map the result from the query directly to the list of strings I supplied in the IN clause:
String Returned ID
------------------------------------------
foo 123
bar NULL <-- Missing row filled with NULL
baz 42
This works fine as long as all strings I pass to the query match a row. When one is missing, however, I would like to fill in the blank with a NULL as in the example above.
Is there any way to accomplish this?
Edit: I should probably have pointed out that the solution must scale to a lot of strings. The way I do it right now is that I pass 100 at a time through the IN clause.
You could do this:
SELECT
helper.SomeColumn,
SomeTable.Id
FROM
(
SELECT 'foo' AS SomeColumn
UNION SELECT 'bar'
UNION SELECT 'baz'
) AS helper
LEFT JOIN SomeTable ON SomeTable.SomeColumn = helper.SomeColumn
Of course you can create the helper table (as a temp table) beforehand instead of inline.
Anyway, maybe it is smarter and more efficient to just do the query you have (WHERE SomeColumn IN (...)) and simply figure out the missing rows in your application. You will loop over them anyway, so you will notice.
What you could do is SELECT the set of strings as a result set and then LEFT JOIN on SomeTable.SomeColumn.
Try this:
SELECT Id
FROM (
SELECT "foo" SomeColumn
UNION ALL
SELECT "bar" AS SomeColumn
UNION ALL
SELECT "baz" AS SomeColumn
) b
LEFT JOIN
SomeTable a
ON a.SomeColumn = b.SomeColumn
What am I doing wrong with this:
$sql = "SELECT * FROM content
WHERE threadName LIKE '%$filter%'
ORDER BY lastUpdated desc
UNION SELECT *
FROM content
WHERE threadName NOT LIKE '%$filter%'
ORDER BY lastUpdated desc";
The first statement before the UNION works well on its own, but this one above returns:
mysql_fetch_array() warning - supplied argument is not a valid MySQL result resource
Am I right in believing that UNION will not return duplicate entries, in which case the second SELECT statement doesn't need to have the NOT LIKE but will just return everything that wasn't listed in the first statement.
EDIT: This query should get you the rows matched by filter first, followed by those not matched:
SELECT *
FROM content
ORDER BY
CASE WHEN threadName LIKE '%$filter%' THEN 0 ELSE 1 END,
lastUpdated DESC
Note that you should never SELECT *, list the necessary columns instead.
While using UNION we must use column names instead of '*'.
Here, I m going to create .csv file at specific location on the system with emailid validation expression by using mysql query as bellow.
select 'col1','col2','col3','col4' from tableName
UNION
SELECT col1,col2,col3,col4 FROM tableName WHERE col4 NOT REGEXP '^[a-zA-Z0-9][a-zA-Z0-9.-]*[a-zA-Z0-9.-]#[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]\.[a-zA-Z]{2,4}$'
INTO OUTFILE '/home/sachin/mysqloutput/data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY ''''
LINES TERMINATED BY '\n';