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
Related
I have selected my data with;
SELECT * FROM item_temp WHERE name LIKE '%starter%' AND Inventory LIKE '600';
I want to duplicate my selected data (Not overwrite it) multiply "entry" of every item in the query by 10.
As an example, the "entry" of one item is: 51327.
I want to make a copy of the item with an entry of 513270.
I have tried a few different methods but they've all resulted in errors and I feel like I'm at a brick wall.
Thanks in advance.
Something like this:
select (it.entry * 10 + n) as entry, . . . -- the rest of the columns go here
from (select 0 as n union all select 1 union all . . . select 9) n cross join
item_temp it
where it.name LIKE '%starter%' AND it.Inventory LIKE '600' ;
Use the INSERT INTO syntax
INSERT INTO table_name
<your query with same column order as table_name>;
Another option is making the destination table ex-novo with select ... into statement
SELECT *
into new_table
FROM item_temp
WHERE name LIKE '%starter%'
AND Inventory LIKE '600';
Use INSERT INTO with a SELECT that does the multiplication you need. You will have to write all columns on for the inserting table.
INSERT INTO item_temp (
entry
-- , other columns
)
SELECT
T.entry * 10 AS entry
-- , other columns
FROM
item_temp T
WHERE
name LIKE '%starter%' AND
Inventory LIKE '600';
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))";
For example I have a table column : lorem,ipsum,mini,momo,testing
I need to select total 'Comma' 's count.
Is it possible to query it as result of : 4 because having 4 comma in the column ?
Does mysql have any trick like select character length by filtering certain words like :
SELECT CHAR_LENGTH(section,',') FROM table WHERE 1 = 1 ?
You are close. Just use subtraction:
select length(section) - length(replace(section, ',', ''))
However, you shouldn't be storing lists of things in strings. Instead, use a junction table with one row per item.
I have a following table
SNo Data
1 |AA|B|C|D|E|
2 |AB|B|C|D|
3 |AA|C|
4 |AA|
5 |AA|AB|AC|C|
6 |AB|B|C|
data is delimited by "|". I understand that the table is denormalized but I cannot change the schema.
The user will give one more more inputs. For example if the user gives input as AA and C I have to retrieve only those rows where only AA and C occurs and not other rows
In this case my output will be
SNo Data
3 |AA|C|
The query that I have tried
Select * from table1 where data like %AA%C% will retrieve rows 1,2,3,5,6
Thanks
Assuming that within a record:
"blank" values are not to be ignored—i.e. |AA||C| is not the same as |AA|C|; and
none of the values are repeated—i.e. |AA|C|AA| will never occur.
Then you can perform pattern matches and test the total value length:
SELECT SNo
FROM my_table
WHERE data LIKE CONCAT('%|', 'AA', '|%')
AND data LIKE CONCAT('%|', 'C' , '|%')
AND CHAR_LENGTH(data) = 1 + CHAR_LENGTH('AA')
+ 1 + CHAR_LENGTH('C' )
+ 1
See it on sqlfiddle.
You can use wildcards, but you have to separate the expressions like this:
SELECT * FROM table1
WHERE data LIKE '%|AA|%' AND data LIKE '%|C|%'
This gets you all the records that contain both |AA| and |C|. However, to limit to rows that match only both:
SELECT * FROM table1
WHERE data LIKE '%|AA|%' AND data LIKE '%|C|%'
AND LENGTH(data) = 6
If you have just two, you may as well key them in and actually use an index on the data column though:
SELECT * FROM table1
WHERE data = '|AA|C|' OR data = '|C|AA|'
Which on older versions of MySQL can be optimized to:
SELECT * FROM table1
WHERE data = '|AA|C|'
UNION ALL
SELECT * FROM table1
WHERE data = '|C|AA|'
Alternatively, you could use a regular expression, which would not utilize an index.
A little idea using regular expressions:
select * from table1 where data regexp '(\\|AA\\|)+.*(\\|C\\|)+';
If I have understood correctly, what you are asking is "get the rows which matches input with delimiter".
So if input is "AA" and "c" then get rows where Data column will have result exactly like
|AA|C|
You can probably do like
select * from table1 where data = select concat('|',concat_ws('|','AA','C'),'|')
$whereClause = array();
foreach( $input AS $search ){
$whereClause[] = sprintf("data REGEXP '|?%s|?'", $search);
}
$sql = "SELECT * FROM tbl WHERE ".implode(" AND ", $whereClause);
Regular Expression Explained
This is the regular expression '|%s|' .
I'm trying do something like this:
SELECT * FROM table WHERE column IN (1,2,3)
but in place of 1,2,3 I want to use a column from another table that contains a comma-delimited list just like "1,2,3" above.
I have tried to do this:
SELECT
GROUP_CONCAT(a.eating_area SEPARATOR ', ')
FROM table_areas a
WHERE a.eating_area_id IN (
SELECT
o.eating_area_ids
FROM table_offers o WHERE o.rid=1
)
however this only returns the value associated with 1, and not 2 or 3. Can this be done or is there another way to do this?
Many thanks
SELECT * FROM table t
WHERE IF(FIND_IN_SET(column,(SELECT "1,2,3" FROM otherTable WHERE 1))>=1,1,0)
-- FIND_IN_SET will return the position.
I don't know if it's the best way to do it but... i think it could work.
Source: Find_in_set