I have a query in MySQL:
select id, 1 from table;
When I run this query I will get the result as below
id | 1
-----|----
5001 | 1
5002 | 1
5003 | 1
Here I'm passing static value as a parameter to the SQL query.
In Place of static value I have a list like
A = [1,2,3]
Size of A is same as the number of rows in the table.
List elements may not be in the incremental order they may be in any random order elements may have strings also but we will have a list.
I want replace static parameter with A. So that it will iterate with list and gives the list elements
I need a query which does like below
for example :
select id , A from table;
Result :
id | A
----|-----
5001| 1
5002| 2
5003| 3
Can someone help me in generating this query?
Try this:
mysql> select id from new_table;
+----+
| id |
+----+
| 1 |
| 2 |
+----+
2 rows in set (0.00 sec)
mysql> SET #row_number = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> select id, #row_number := #row_number + 1, ELT(#row_number, '1111', 2222) AS A from new_table;
+----+--------------------------------+------+
| id | #row_number := #row_number + 1 | A |
+----+--------------------------------+------+
| 1 | 1 | 1111 |
| 2 | 2 | 2222 |
+----+--------------------------------+------+
2 rows in set (0.00 sec)
Related
I created a column called oilcompany that has SET data (Hunt, Pioneer, Chevron, BP)
I can enter any one of those into the oilcompany column and change from one to another one but I can not figure out how to change from one oilcompany to multiple oilcompany (eg. Hunt and BP)... any suggestion?
In the MySQL documentation there are not examples for UPDATE statements, but I normally use two ways to update these kind of columns:
Using text values
Using numeric values
Creating the test environment
mysql> CREATE TABLE tmp_table(
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> oilcompany SET('Hunt', 'Pioneer', 'Chevron', 'BP')
-> );
Query OK, 0 rows affected (0.54 sec)
mysql> INSERT INTO tmp_table(oilcompany) VALUES ('Hunt'), ('Pioneer');
Query OK, 2 rows affected (0.11 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM tmp_table;
+----+------------+
| id | oilcompany |
+----+------------+
| 1 | Hunt |
| 2 | Pioneer |
+----+------------+
2 rows in set (0.00 sec)
Alternative#1: Using Text Values
As a SET is a collection of ENUM elements, and any ENUM element can be treated as a string, then we can do things like:
mysql> UPDATE tmp_table
-> SET oilcompany = 'Hunt,BP'
-> WHERE id = 1;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM tmp_table;
+----+------------+
| id | oilcompany |
+----+------------+
| 1 | Hunt,BP |
| 2 | Pioneer |
+----+------------+
2 rows in set (0.00 sec)
Alternative#2: Using Numeric Values
Any SET element is stored internally as a 64bit number containing the combination of the bits that represent each SET element.
In our table: 'Hunt'=1, 'Pioneer'=2, 'Chevron'=4, 'BP'=8.
Also, mysql allows to use these numbers instead of text values. If we need to see the numeric value in the select, we need to use the SET column inside a numeric expression (E.g. adding zero).
Let's see the current values:
mysql> SELECT id, oilcompany+0, oilcompany FROM tmp_table;
+----+--------------+------------+
| id | oilcompany+0 | oilcompany |
+----+--------------+------------+
| 1 | 9 | Hunt,BP |
| 2 | 2 | Pioneer |
+----+--------------+------------+
2 rows in set (0.00 sec)
Here 9 = 'Hunt' (1) + 'BP' (8) and 2 = 'Pioneer' (2).
Now, let's change the Pioneer to 'Hunt' (1) + 'Chevron' (4):
mysql> UPDATE tmp_table
-> SET oilcompany = 5
-> WHERE id = 2;
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT id, oilcompany+0, oilcompany FROM tmp_table;
+----+--------------+--------------+
| id | oilcompany+0 | oilcompany |
+----+--------------+--------------+
| 1 | 9 | Hunt,BP |
| 2 | 5 | Hunt,Chevron |
+----+--------------+--------------+
2 rows in set (0.00 sec)
I have one table
test
ID text sum
-----------------------
1 1_2_3 0
2 2_3_4_5 0
i want to update this table as
ID text sum
------------------------
1 1_2_3 6
2 2_3_4_5 14
how to write the query or function/procedure.
You should really NORMALIZE your data,but assuming you are forced to work with it:
UPDATE tableName SET sum=SUBSTRING_INDEX(text,'_',1) +
SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(text,'_0'),'_',2),'_',-1) +
SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(text,'_0'),'_',3),'_',-1) +
SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(text,'_0'),'_',4),'_',-1) +
SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(text,'_0'),'_',5),'_',-1);
Use SUBSTRING _INDEX to isolate each number,CONCAT is used to give a 0 if the number of expressions is larger than the number of values.
The fiddle
Try with this solution...
Hope this will help you....
SELECT SUM(Trim( Left(Name, InStr(Name, "_") - 1)) +
Trim( Mid(Name, InStr(Name, "_") + 1)) +
Trim(Right(Name, InStr(Name, ",") + 1))) as SUM FROM TEST;
Where the table structure is like this:
Id | Name |
1 | 1_2_3 |
2 | 5_8_10 |
It's resulted as
SUM
6
23
The best way to go about this is to make each text field an SQL statement.
First, here is sample data
mysql> drop table if exists prabhu;
Query OK, 0 rows affected (0.27 sec)
mysql> create table prabhu
-> (
-> id int not null auto_increment primary key,
-> text varchar(128),
-> sum int default 0
-> );
Query OK, 0 rows affected (0.56 sec)
mysql> insert into prabhu (text) values ('1_2_3'),('2_3_4_5');
Query OK, 2 rows affected (0.09 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from prabhu;
+----+---------+------+
| id | text | sum |
+----+---------+------+
| 1 | 1_2_3 | 0 |
| 2 | 2_3_4_5 | 0 |
+----+---------+------+
2 rows in set (0.00 sec)
mysql>
Here is a query to make each row produce an SQL statement to update the sum column
mysql> SELECT CONCAT('UPDATE prabhu SET sum=',
-> REPLACE(text,'_','+'),' WHERE id=',id,';') sqlstmt FROM prabhu;
+-------------------------------------------+
| sqlstmt |
+-------------------------------------------+
| UPDATE prabhu SET sum=1+2+3 WHERE id=1; |
| UPDATE prabhu SET sum=2+3+4+5 WHERE id=2; |
+-------------------------------------------+
2 rows in set (0.00 sec)
mysql>
Now, pipe the output of the query back into mysql and execute each line
C:\>mysql -Dtest -ANe"SELECT CONCAT('UPDATE prabhu SET sum=',REPLACE(text,'_','+'),' WHERE id=',id,';') sqlstmt FROM pra
bhu" | mysql -Dtest
C:\>mysql -Dtest -Ae"SELECT * FROM prabhu"
+----+---------+------+
| id | text | sum |
+----+---------+------+
| 1 | 1_2_3 | 6 |
| 2 | 2_3_4_5 | 14 |
+----+---------+------+
C:\>
Give it a Try !!!
For example this is myTable:
| i | data |
+---+---------+
| 1 | d\one |
| 2 | d\two |
| 3 | d\three |
But I want to change it to:
| i | data |
+---+-------+
| 1 | one |
| 2 | two |
| 3 | three |
I know how to find a specific part of a string with a backslash in a field:
SELECT * FROM myTable WHERE data LIKE 'd%\%'
I know how to find & replace a field a value, only this query won't work:
UPDATE myTable SET data=REPLACE(data,'d\','') WHERE data LIKE 'd%\%'
You need to escape the backslash
UPDATE myTable
SET data = REPLACE(data,'d\\','')
WHERE data INSTR('d\\') = 1
SQLFiddle demo
I hope you should put triple slash \\\ instead of double slash \\ in Like Clause. As I have tried I am unable to pick data from using \\
SELECT * FROM mytable WHERE data LIKE 'd\\%';
Empty set (0.00 sec)
mysql> SELECT * FROM mytable WHERE data LIKE 'd\\\%';
+---------+
| data |
+---------+
| d\one |
| d\two |
| d\three |
+---------+
3 rows in set (0.00 sec)
mysql> SELECT REPLACE(data,'d\\','') FROM mytable WHERE data LIKE 'd\\\%';
+------------------------+
| REPLACE(data,'d\\','') |
+------------------------+
| one |
| two |
| three |
+------------------------+
3 rows in set (0.00 sec)
mysql> UPDATE mytable SET data = REPLACE(data,'d\\','') WHERE data LIKE 'd\\\%';
Query OK, 3 rows affected (0.06 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> SELECT * FROM mytable;
+-------+
| data |
+-------+
| one |
| two |
| three |
+-------+
3 rows in set (0.00 sec)
try this one..
UPDATE myTable SET `data`=REPLACE(`data`,'d\','');
I'm testing the InfiniDB community edition to see if it suits our needing.
I imported in a single table about 10 millions rows (loading of data was surprisingly fast), and I'm trying to do some query on it, but these are the results (with NON cached queries.. if query caching exists in InfiniDB):
Query 1 (very fast):
select * from mytable limit 150000,1000
1000 rows in set (0.04 sec)
Query 2 (immediate):
select count(*) from mytable;
+----------+
| count(*) |
+----------+
| 9429378 |
+----------+
1 row in set (0.00 sec)
Ok it seems to be amazingly fast.. but:
Query 3:
select count(title) from mytable;
.. still going after several minutes
Query 4:
select id from mytable where id like '%ABCD%';
+------------+
| id |
+------------+
| ABCD |
+------------+
1 row in set (11 min 17.30 sec)
I must be doing something wrong, it's not possible that it's performing this way with so simple queries. Any Idea?
That shouldn't be the case, there does appear to be something odd going on, see quick test below.
What is your server configuration: memory/OS/CPU and platform (dedicated, virtual, cloud).
Could I get the schema declaration and method to load the data?
Which version are you using? Version 4 community has significantly more features than prior versions, i.e. core syntax matches enterprise.
Cheers,
Jim T
mysql> insert into mytable select a, a from (select hex(rand() * 100000) a from lineitem limit 10000000) b;
Query OK, 10000000 rows affected (1 min 54.12 sec)
Records: 10000000 Duplicates: 0 Warnings: 0
mysql> desc mytable;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | varchar(32) | YES | | NULL | |
| title | varchar(32) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> select * from mytable limit 150000,1000;
+-------+-------+
| id | title |
+-------+-------+
| E81 | E81 |
| 746A | 746A |
. . .
| DFC8 | DFC8 |
| 2C56 | 2C56 |
+-------+-------+
1000 rows in set (0.07 sec)
mysql> select count(*) from mytable;
+----------+
| count(*) |
+----------+
| 10000000 |
+----------+
1 row in set (0.06 sec)
mysql> select count(title) from mytable;
+--------------+
| count(title) |
+--------------+
| 10000000 |
+--------------+
1 row in set (0.09 sec)
mysql> select id from mytable where id like '%ABCD%' limit 1;
+------+
| id |
+------+
| ABCD |
+------+
1 row in set (0.03 sec)
Taking my first steps with MySQL, I noticed that the character positions counts from 1 and the start row counts from 0, and I became curious about why it processes this way.
For example, say I have a table called users.
mysql> SELECT * FROM users;
+----+------------+
| id | name |
+----+------------+
| 1 | User One |
| 2 | User Two |
| 3 | User Three |
+----+------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM users LIMIT 1, 2;
+----+------------+
| id | name |
+----+------------+
| 2 | User Two |
| 3 | User Three |
+----+------------+
2 rows in set (0.00 sec)
The start row counts from zero, so 1 is actually the second row (User Two).
mysql> SELECT substring( 'Hello, world!', 2, 4 );
+------------------------------------+
| substring( 'Hello, world!', 2, 4 ) |
+------------------------------------+
| ello |
+------------------------------------+
1 row in set (0.00 sec)
The character positions start from 1, rather than zero.
I tend to think it's done this way for a purpose and I'd like to know what is the reason for this difference.