INSERT / UPDATE SQL random & unique VARCHAR - mysql

I need to be able to INSERT/UPDATE UNIQUE RANDOM UTF8 ALPHANUMERICAL VARCHAR 55 into a table field called 'key'.
Can't find out any good query example, does anyone can show me or link me something?

This answer is based on mysql.
This select will create 55 char long random strings:
select substr(concat(md5(rand()),md5(rand())),1,55);
to fill your table column you might want to try out:
create table example (keycol varchar(55));
insert into example (keycol) values (substr(concat(md5(rand()),md5(rand())),1,55));
The result will be:
select keycol from example;
+---------------------------------------------------------+
| keycol |
+---------------------------------------------------------+
| 4517f4be669301a4a529b53fc18d646dec42d4d07d911d33a67c863 |
| 3caa1c98f0f9ee39515aa6f4ddb3f84fa41abd5392f610c5d24bcd9 |
| 8e52cb4ce29e58514671c9b68f19832f26ddf53f277621ac420bd2e |
| 3adcccfb6cb729ce1c0a14fb75f6fd54f58992dc0751527c969e007 |
| c28c5879589dc90f4fb0963673e5668fa5789d325423ba043e0243b |
| 8f7a2af97d73261008f0d0d7249480fde56a3a91f2ce6e8bf0b0070 |
| ff4f74f25b92da3eaab282218c23a75d4cfa77c8f8bfdf74d7ebdf9 |
+---------------------------------------------------------+

Related

MYSQL - add new column with default string concat with data from existing column

I need to insert a new column of URLs into a table of items based on their ROM code that already exists in a column of my table.
my URL needs to have the following format
/media/marquees/{ROM}.png
| rom | marquee_url |
|---------------------------------------|
| abcd | /media/marquees/abcd.png |
| efg | /media/marquees/efg.png |
I'm fairly confident there are no NULL values in _rom
You can use generated columns in 5.7.
> CREATE TABLE test_table ( rom varchar(20), marquee_url varchar(30) AS (concat("/media/marquees/",rom,".png")) );
> insert into test_table (rom) values ('abcd'), ('efg');
> select * from test_table ;
+------+--------------------------+
| rom | marquee_url |
+------+--------------------------+
| abcd | /media/marquees/abcd.png |
| efg | /media/marquees/efg.png |
+------+--------------------------+`
For more info on how generated columns work, please refer to this link https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html
user concat for marquee_url field you get result:
INSERT INTO table_name (room, marquee_url) VALUES (value1, CONCAT( '/media/marquees/',value1))

How to SELECT values if id available in column(Comma separated values) using mysql?

How to SELECT values if id available in column(Comma separated values) using MySQL?
Here I need to get all values when the given id=17 available in group_id column.
Table:
+------------+---------------+
| user_id | group_id |
+------------+---------------+
| 1 | 1,2,3 |
| 3 | 12,23,17 |
| 5 | 17,26 |
+------------+---------------+
I try:
SELECT * FROM `group` WHERE units_id IN('17'); //No result
Expecting result:
+------------+---------------+
| user_id | group_id |
+------------+---------------+
| 3 | 12,23,17 |
| 5 | 17,26 |
+------------+---------------+
You can use FIND_IN_SET
SELECT * FROM `group` WHERE FIND_IN_SET(17,group_id);
Note: It's highly discouraged to store comma separated values in column. A must read:
Is storing a delimited list in a database column really that bad?
Yes
Also you shouldn't use MySQL reserved words as your identifer's name. Be careful to enclose by backtick while using.
Try this one.
You can use find_in_set :
SELECT * FROM `user` WHERE find_in_set('17',group_id) ORDER BY user_id;
RESULT:
+------------+---------------+
| user_id | group_id |
+------------+---------------+
| 3 | 12,23,17 |
| 5 | 17,26 |
+------------+---------------+
REF: MySQL query finding values in a comma separated string
You can use ´find_in_set`
SELECT * FROM `group` WHERE find_in_set('17',units_id );
IN checks if a column contains values from a comma separated list
It is very bad db design if you store values as csv.
For more infoemartion see mysql documentation

Deleting almost duplicate rows in MySQL?

I have seen a few different answers for this question, but none really hit exactly what I needed to do in MySQL.
I did find a thread for MS SQL that is exactly to what I need to do here but nothing min MySQL.
Data Example
+--------+----------+--------+
| Col1 | Col2 | UniqueID |
+--------+----------+--------+
| Peaches| Outdoor | 1 |
| Peaches| Outdoor | 2 |
| Apples | Indoor | 3 |
| Apples | Indoor | 4 |
+--------+----------+--------+
Desired Output
+--------+----------+--------+
| Col1 | Col2 | UniqueID |
+--------+----------+--------+
| Peaches| Outdoor | 1 |
| Apples | Indoor | 3 |
+--------+----------+--------+
Your way is OK. You only forgot the KEYWORD TABLE
CREATE TABLE NewTable AS SELECT Col1,Col2 ,MAX(col3) FROM t GROUP BY Col1,col2
but the structure can be different from the original table
Do this way:
CREATE TABLE NewTable like t;
then add a unique key:
ALTER TABLE NewTable ADD KEY (Col1,col2);
and now copy old data in new table with ON DUPLICATE KEY UPDATE
INSERT INTO NewTable
SELECT *
from t
ON DUPLICATE KEY UPDATE Col3=GREATEST(Col3,VALUES(Col3));
so you copy every row and the duplicates tests for maximum
Im going to post the answer to the answer provided above so its clear...it is just one simple query:
CREATE NewTable AS SELECT Col1,Col2 ,MAX(col3) FROM t GROUP BY Col1,col2
Just querying max was the trick...so simple.
Thank you!

MySQL recursive update based on values in the same table

I am having trouble implementing the following structure in MySQL.
Table1:
ID | Val
1 | 10
2 | 20
Table2:
ID | LeftTableType | LeftID | LeftVal | RightTableType | RightID | RightVal | Operation | Result
1 | Table1 | 1 | (10) | Table1 | 2 | (20) | + | (30)
2 | Table2 | 1 | (30) | Table1 | 2 | (20) | + | (50)
I tried to use a trigger system where an update to Table1 would update the values of Table2. Unfortunately, I needed to then update subsequent values of Table2, which caused a recursive trigger system that MySQL did not like.
I have also been looking into nested sets and tree structures. It seems like they might be what I am looking for, or at least very close.
Is there something obvious that I am missing to implement something like this? This seems like it might lead me to a messy mixture of cursors, recursion, triggers, procedures, and tree structures.
Any hints would be greatly appreciated!

mysql insert external data with join

I'm usually pretty resourceful, but I'm stuck on this one. Any help would be appreciated.
Say I've got a table for produce, like this, including counts of sold/in stock for each produce type.
+--------------+--------------+------+-----+
| Field | Type | Null | Key |
+--------------+--------------+------+-----+
| produce_type | varchar(100) | NO | PRI |
| sold_count | int(8) | YES | |
| stock_count | int(8) | YES | |
+--------------+--------------+------+-----+
I'm doing a separate insert using external data for each of the 'stock' and 'sold' counts, with hundreds to thousands of produce_types at a time. I may have data with a given produce_type existing only in the 'stock' or 'sold' data to be inserted, but want all to be present in the table.
So, e.g., doing one insert for sold_count ('potato', 3), ('onion', 5) and one for stock_count ('potato', 8), ('carrots', 6), I'd want to end up with this:
+--------------+------------+-------------+
| produce_type | sold_count | stock_count |
+--------------+------------+-------------+
| potato | 3 | 8 |
| onion | 5 | NULL |
| carrots | NULL | 6 |
+--------------+------------+-------------+
So I'd need to join to existing data upon the second column's insert statement, but all I see here or elsewhere on the web is instructions for joins when inserting from another table.
INSERT IGNORE doesn't do it, as one of the 'potato' columns wouldn't get written to.
INSERT ... ON DUPLICATE KEY UPDATE gets closer but I can't figure out how to set the update field to the value from the dataset I'm inserting.
Do I need to create a temp table for the 2nd insert (+ outer join)? Any structurally simpler way of doing this?
Thanks in advance.
Edit: I think I can probaly use this:
https://stackoverflow.com/a/3466/2540707
Does this work?
insert into produce ( produce_type, sold_count )
select produce_type, sold_count from sold_data
on duplicate key update sold_count = ( select sold_count from sold_data
where produce.produce_type = sold_data.produce_type
);