output mysql> insert into test (id,img)
values(1,LOAD_FILE('/home/ubundu/pic.jpg'));
Query OK, 1 row affected (0.55 sec)
mysql> select * from test;
+------+------+
| id | img |
+------+------+
| 1 | NULL |
+------+------+
1 row in set (0.02 sec)
Related
I have a mysql table with 2 entries (id ,title) (int, text)
A row of dummy data in the table looks like
id title
1 apple\"apple
My query intends to do a search on the field title
select * from table1 where title='apple\"apple';
I also tried using
select * from table1 where title like('apple\"apple');
The above query did not seem to work as well.
Both queries are returning an empty sets. Is it not possible to search for a string that has backslash in it?
PS :: The \ is added to the DB as part of escaping the double quotes.
Sure? It works as expected.
mysql> CREATE TABLE MyTbl (id INT, title TEXT);
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO MyTbl VALUES(1,'apple "apple');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM MyTbl;
+------+-------------+
| id | title |
+------+-------------+
| 1 | apple"apple |
+------+-------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM MyTbl WHERE title = 'apple \"apple';
+------+-------------+
| id | title |
+------+-------------+
| 1 | apple"apple |
+------+-------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM MyTbl WHERE title like('apple \"apple');
+------+-------------+
| id | title |
+------+-------------+
| 1 | apple"apple |
+------+-------------+
1 row in set (0.00 sec)
Maybe Is it something related to the space character?
mysql> SELECT * FROM MyTbl WHERE title = 'apple\"apple';
Empty set (0.00 sec)
mysql> SELECT * FROM MyTbl WHERE title = 'apple \"apple';
+------+--------------+
| id | title |
+------+--------------+
| 1 | apple "apple |
+------+--------------+
1 row in set (0.00 sec)
What about running this command? What It returns for you?
mysql> SELECT #NO_BACKSLASH_ESCAPES;
+----------------------------------------------+
| #NO_BACKSLASH_ESCAPES |
+----------------------------------------------+
| NULL |
+----------------------------------------------+
1 row in set (0.00 sec)
In MySQL I have some tables I need to randomize the phone numbers and Email addresses to be randomly generated for development purposes.
In MySQL how could I generate 7 digit unique random numbers for the phone numbers?
How can I generate random email address like 545165498#mailinator.com.
How can I generate this random data with MySQL Queries?
MySQL rand() Returns a random floating-point value in the range 0 <= value < 1.0.
Multiply that by another number: UPPER_BOUND and get the floor of that, and you will get a random integer between 0 and (UPPER_BOUND-1) like this:
SELECT floor(rand() * 10) as randNum;
That will give you only one random number between 0 and 10.
Change the 10 to the number one higher than you want to generate.
Something like this :
UPDATE user
SET email = CONCAT(FLOOR(rand() * 10000000),'#mailinator.com'),
PhoneNo = FLOOR(rand() * 10000000)
This should give you a random number of 7 digits length
SELECT FLOOR(1000000 + RAND() * 8999999)
And something like this should update your phone numbers and e-mail addresses according to your requirement
UPDATE Customers
SET phone = CAST(FLOOR(1000000 + RAND(8999999) AS VARCHAR),
email = CONCAT(CAST(FLOOR(1000000 + RAND(8999999) AS VARCHAR), '#mailinator.com')
MySQL Generate random data walkthrough:
Random number between 0 (inclusive) and 1 exclusive:
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.5485130739850114 |
+--------------------+
1 row in set (0.00 sec)
Random int between 0 (inclusive) and 10 exclusive:
mysql> select floor(rand()*10);
+------------------+
| floor(rand()*10) |
+------------------+
| 6 |
+------------------+
1 row in set (0.00 sec)
Random letter or number:
mysql> select concat(substring('ABCDEF012345', rand()*36+1, 1));
+---------------------------------------------------------------------------+
| concat(substring('ABCDEF012345', rand()*36+1, 1)) |
+---------------------------------------------------------------------------+
| F |
+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
Random letter a to z:
mysql> select char(round(rand()*25)+97);
+---------------------------+
| char(round(rand()*25)+97) |
+---------------------------+
| s |
+---------------------------+
1 row in set (0.00 sec)
Random 8 character alphanumeric string:
mysql> SELECT LEFT(UUID(), 8);
+-----------------+
| LEFT(UUID(), 8) |
+-----------------+
| c26117af |
+-----------------+
1 row in set (0.00 sec)
Random capital letter in MySQL:
mysql> select CHAR( FLOOR(65 + (RAND() * 25)));
+----------------------------------+
| CHAR( FLOOR(65 + (RAND() * 25))) |
+----------------------------------+
| B |
+----------------------------------+
1 row in set (0.00 sec)
Load a random row into a table:
mysql> create table penguin (id INT primary key auto_increment, msg TEXT);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into penguin values (0, LEFT(UUID(), 8));
Query OK, 1 row affected (0.00 sec)
mysql> select * from penguin;
+------+----------+
| id | msg |
+------+----------+
| 0 | abab341b |
+------+----------+
1 row in set (0.00 sec)
Load random rows:
Make a procedure called dennis that loads 1000 random rows into penguin.
mysql> delimiter ;;
mysql> drop procedure if exists dennis;;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create procedure dennis()
-> begin
-> DECLARE int_val INT DEFAULT 0;
-> myloop : LOOP
-> if (int_val = 1000) THEN
-> LEAVE myloop;
-> end if;
-> insert into penguin values (0, LEFT(UUID(), 8));
-> set int_val = int_val +1;
-> end loop;
-> end;;
Query OK, 0 rows affected (0.00 sec)
mysql> call dennis();;
mysql> select * from penguin;;
+------+----------+
| id | msg |
+------+----------+
| 0 | abab341b |
| 1 | c5dc08ee |
| 2 | c5dca476 |
...
+------+----------+
Update all rows in a table to have random data:
mysql> create table foo (id INT primary key auto_increment, msg TEXT);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into foo values (0,'hi');
Query OK, 1 row affected (0.00 sec)
mysql> insert into foo values (0,'hi2');
Query OK, 1 row affected (0.00 sec)
mysql> insert into foo values (0,'hi3');
Query OK, 1 row affected (0.00 sec)
mysql> select * from foo;
+----+------+
| id | msg |
+----+------+
| 1 | hi |
| 2 | hi2 |
| 3 | hi3 |
+----+------+
3 rows in set (0.00 sec)
mysql> update foo set msg = rand();
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from foo;
+----+---------------------+
| id | msg |
+----+---------------------+
| 1 | 0.42576668451145916 |
| 2 | 0.6385560879842901 |
| 3 | 0.9154804171207178 |
+----+---------------------+
3 rows in set (0.00 sec)
Here is an online tool to generate random data with many options. http://www.generatedata.com/
Just enter the parameters to define what kind of random data you want, and export it to the appropriate format, then you can load it.
I think I read that the delete trigger doesn't know what data was deleted and loops over the whole table applying the trigger. Is that true?
Does that mean that the before delete loops over the whole table before the data is deleted and after delete loops over the whole table after the delete occurs?
Is there no way to loop over just the deleted records? So If 10 records are deleted loop over them?
DELIMITER $$
DROP TRIGGER `before_delete_jecki_triggername`$$
CREATE TRIGGER before_delete_triggername
BEFORE DELETE ON table
FOR EACH ROW
BEGIN
/*do stuff*/
END$$
DELIMITER ;
Thanks,
Mat
I think it was due to a confusion with FOR EACH ROW statement.
It is only for "matched records for the statement issued before trigger is invoked."
If there exists N number of records in a table and matches records for where id=x,
assuming x causes a result of less than N records, say N-5, then
FOR EACH ROW causes a loop for N-5 times only.
UPDATE:
A sample test run on the rows affected due to FOR EACH ROW statement is shown below.
mysql> -- create a test table
mysql> drop table if exists tbl; create table tbl ( i int, v varchar(10) );
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.06 sec)
mysql> -- set test data
mysql> insert into tbl values(1,'one'),(2,'two' ),(3,'three'),(10,'ten'),(11,'eleven');
Query OK, 5 rows affected (0.02 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from tbl;
+------+--------+
| i | v |
+------+--------+
| 1 | one |
| 2 | two |
| 3 | three |
| 10 | ten |
| 11 | eleven |
+------+--------+
5 rows in set (0.02 sec)
mysql> select count(*) row_count from tbl;
+-----------+
| row_count |
+-----------+
| 5 |
+-----------+
1 row in set (0.00 sec)
mysql>
mysql> -- record loop count of trigger in a table
mysql> drop table if exists rows_affected; create table rows_affected( i int );
Query OK, 0 rows affected (0.02 sec)
Query OK, 0 rows affected (0.05 sec)
mysql> select count(*) 'rows_affected' from rows_affected;
+---------------+
| rows_affected |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec)
mysql>
mysql> set #cnt=0;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> -- drop trigger if exists trig_bef_del_on_tbl;
mysql> delimiter //
mysql> create trigger trig_bef_del_on_tbl before delete on tbl
-> for each row begin
-> set #cnt = if(#cnt is null, 1, (#cnt+1));
->
-> /* for cross checking save loop count */
-> insert into rows_affected values ( #cnt );
-> end;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> delimiter ;
mysql>
mysql> -- now let us test the delete operation
mysql> delete from tbl where i like '%1%';
Query OK, 3 rows affected (0.02 sec)
mysql>
mysql> -- now let us see what the loop count was
mysql> select #cnt as 'cnt';
+------+
| cnt |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
mysql>
mysql> -- now let us see the table data
mysql> select * from tbl;
+------+-------+
| i | v |
+------+-------+
| 2 | two |
| 3 | three |
+------+-------+
2 rows in set (0.00 sec)
mysql> select count(*) row_count from tbl;
+-----------+
| row_count |
+-----------+
| 2 |
+-----------+
1 row in set (0.00 sec)
mysql> select count(*) 'rows_affected' from rows_affected;
+---------------+
| rows_affected |
+---------------+
| 3 |
+---------------+
1 row in set (0.00 sec)
mysql>
we have an issue because this is how the table was setup. How do we select from a table where a set does not contain a value? If we have a set with one,two and we do
SELECT *
FROM table
WHERE column NOT IN('one')
we will still get that row. What is the correct syntax for this?
Thanks
this is what worked:
SELECT *, FIND_IN_SET('hidden', props) As hidden
FROM gt_content
HAVING hidden IS NULL
Try:
SELECT *
FROM table
WHERE FIND_IN_SET('one',column) = 0
Some test I did:
mysql> CREATE TABLE setTest (s SET('a','b','c','d'));
Query OK, 0 rows affected (0.60 sec)
mysql> INSERT INTO setTest VALUES ('a'),('a,b'),('b'),('a,c'),('c,d');
Query OK, 5 rows affected (0.14 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM setTest;
+------+
| s |
+------+
| a |
| a,b |
| b |
| a,c |
| c,d |
+------+
5 rows in set (0.20 sec)
mysql> SELECT * FROM setTest WHERE FIND_IN_SET('a',s) = 0;
+------+
| s |
+------+
| b |
| c,d |
+------+
2 rows in set (0.12 sec)
mysql> SELECT * FROM setTest WHERE FIND_IN_SET('b',s) = 0;
+------+
| s |
+------+
| a |
| a,c |
| c,d |
+------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM setTest WHERE FIND_IN_SET('b',s) > 0;
+------+
| s |
+------+
| a,b |
| b |
+------+
2 rows in set (0.04 sec)
From the mysql manual at http://dev.mysql.com/doc/refman/5.0/en/set.html:
Normally, you search for SET values using the FIND_IN_SET() function or the LIKE operator:
SELECT * FROM tbl_name WHERE FIND_IN_SET('value',set_col)>0;
SELECT * FROM tbl_name WHERE set_col LIKE '%value%';
The first statement finds rows where set_col contains the value set member. The second is similar, but not the same: It finds rows where set_col contains value anywhere, even as a substring of another set member.
Can there any way to insert a hex value into MYSQL?
I also want to be able to retreive it in hex form.
For example, something like:
INSERT INTO table ( hexTag )
VALUES ( HEX(0x41) );
And if I do this, I want it to put an 'A' into the table
For that particular use case, you can either insert the hex value directly and it will be interpreted as a string, or use HEX() to input and UNHEX() to output
mysql> create table hexTable(pseudoHex varchar(50));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into hexTable values (0x41);
Query OK, 1 row affected (0.00 sec)
mysql> select * from hexTable;
+-----------+
| pseudoHex |
+-----------+
| A |
+-----------+
1 row in set (0.00 sec)
mysql> select HEX(pseudoHex) from hexTable;
+----------------+
| HEX(pseudoHex) |
+----------------+
| 41 |
+----------------+
1 row in set (0.00 sec)
mysql> delete from hexTable;
Query OK, 1 row affected (0.00 sec)
mysql> insert into hexTable values (HEX('A'));
Query OK, 1 row affected (0.00 sec)
mysql> select UNHEX(pseudoHex) from hexTable;
+------------------+
| UNHEX(pseudoHex) |
+------------------+
| A |
+------------------+
1 row in set (0.00 sec)
mysql> select * from hexTable;
+-----------+
| pseudoHex |
+-----------+
| 41 |
+-----------+
1 row in set (0.00 sec)
See these links.