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';
Related
So I have an database where I have a string in each row eg: 1,4,56,3,23
I want to find every row where say 1 is in it, so this row would be found a 1 is at the start but if I have 4,1,54,32,2 it wont find it.
This is the code I'm using:
WHERE ".$id." IN ( outcomes )
You can use LIKE because you have a string separated by comma.
SELECT * FROM users WHERE outcome LIKE '41,%' OR outcome LIKE '%,41,%' OR outcome LIKE '%,41';
The above code select users which have outcome 41 in the list.
Use FIND_IN_SET :
SELECT *
FROM table
where FIND_IN_SET(".$id.",outcomes);
You can use the INSTR function as well :
SELECT *
FROM table
WHERE INSTR(".$id.",'1') > 0
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 a table in which one column is filled with data like 32;3;13;33;43
so
SELECT * FROM table;
gives something like
name ids
vegetables 13;3;63
fruits 37;73;333
When I'm querying MySQL like
SELECT * FROM table WHERE ids LIKE '%3%'
it gives me both records but obviously I want only this containing 3.
How to query MySQL correctly?
Try to use:
SELECT * FROM table WHERE CONCAT(';',ids,';') LIKE '%;3;%'
You will need to cover the case where it's the first in the list and the last.
SELECT * FROM table WHERE ids LIKE '%;3;%' OR LIKE '%;3' OR LIKE '3;%'
You can use FIND_IN_SET, if you replace the ; with a , before checking the value:0
SELECT *
FROM table
WHERE FIND_IN_SET('3', REPLACE(ids, ';', ','))
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