Create function with mysql - mysql

Hello I have an assignment:
Consider an employee database with two relations:
employee(employee-name, street, city)
works(employee-name, company-name, salary)
where the primary keys are underlined. Write a query to find companies whose employees earn a lower salary, on average, than the average salary at “First Bank Corporation”.
Use user defined SQL functions (create function command) as appropriate to answer the above query, the function takes the company name as the input and returns the average salary of the given
company.
I created this function:
create function avg_salary (c_name varchar(30))
returns numeric(8,6)
begin
declare a_salary numeric(8,6);
select avg(salary) into a_salary
from works
where company.company_name = c_name
group by company_name;
return a_salary;
select company_name
from works
where a_salary<(select avg(salary) from works where company_name = ’ First Bank Corporation’);
end
But I get an error message:
Error 1415 (0A000) :Not allowed to return a result set from a function
I don't understand why I get this error.
Thank you for your help

You should read the mysql manual in conjunction with your tutorial to understand mysql specifics.
The error message is clear enough (every select returns a result set) so select company_name
from works is at fault.
Selects into and set = (select...) are fine.
In addition to the error the single quote around ’ First Bank Corporation’ is odd and errors when I try to compile the function.
Also the group by company_name; will be rejected in later versions of my sql because company_name is not included in the select list. see https://dev.mysql.com/doc/refman/8.0/en/group-by-handling.html - in this case it's not needed because you are avgerageing for a specified company.
Mysql also requires delimiters to be set before and after the create function where there are multiple statements in the function see https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html

Try this (example includes all code I used to create all objects and insert data):
DROP TABLE IF EXISTS employee;
CREATE TABLE IF NOT EXISTS employee (
`employee-name` varchar(100) NOT NULL,
`street` varchar(100) NOT NULL,
`city` varchar(100) NOT NULL,
PRIMARY KEY (`employee-name`)
);
DROP TABLE IF EXISTS works;
CREATE TABLE IF NOT EXISTS works (
`employee-name` varchar(100) NOT NULL,
`company-name` varchar(100) NOT NULL,
`salary` numeric(8,2) NOT NULL,
PRIMARY KEY (`employee-name`)
);
INSERT INTO
employee
(`employee-name`, `street`, `city`)
VALUES
('Employee 01', '1234 State ST.', 'Your City'),
('Employee 02', '1234 State ST.', 'Your City'),
('Employee 03', '1234 State ST.', 'Your City'),
('Employee 04', '1234 State ST.', 'Your City'),
('Employee 05', '1234 State ST.', 'Your City'),
('Employee 06', '1234 State ST.', 'Your City'),
('Employee 07', '1234 State ST.', 'Your City'),
('Employee 08', '1234 State ST.', 'Your City'),
('Employee 09', '1234 State ST.', 'Your City'),
('Employee 10', '1234 State ST.', 'Your City'),
('Employee 11', '1234 State ST.', 'Your City'),
('Employee 12', '1234 State ST.', 'Your City'),
('Employee 13', '1234 State ST.', 'Your City'),
('Employee 14', '1234 State ST.', 'Your City'),
('Employee 15', '1234 State ST.', 'Your City'),
('Employee 16', '1234 State ST.', 'Your City'),
('Employee 17', '1234 State ST.', 'Your City'),
('Employee 18', '1234 State ST.', 'Your City'),
('Employee 19', '1234 State ST.', 'Your City'),
('Employee 20', '1234 State ST.', 'Your City'),
('Employee 21', '1234 State ST.', 'Your City'),
('Employee 22', '1234 State ST.', 'Your City');
INSERT INTO
works
(`employee-name`, `company-name`, `salary`)
VALUES
('Employee 01', 'Company 01', 10000.00),
('Employee 02', 'Company 01', 10000.00),
('Employee 03', 'Company 01', 10000.00),
('Employee 04', 'Company 01', 10000.00),
('Employee 05', 'Company 01', 10000.00),
('Employee 06', 'Company 02', 15000.00),
('Employee 07', 'Company 02', 15000.00),
('Employee 08', 'Company 02', 15000.00),
('Employee 09', 'Company 03', 20000.00),
('Employee 10', 'Company 03', 20000.00),
('Employee 11', 'Company 03', 20000.00),
('Employee 12', 'Company 03', 20000.00),
('Employee 13', 'Company 03', 20000.00),
('Employee 14', 'Company 03', 20000.00),
('Employee 15', 'Company 03', 20000.00),
('Employee 16', 'Company 04', 60000.00),
('Employee 17', 'Company 04', 60000.00),
('Employee 18', 'Company 04', 60000.00),
('Employee 19', 'First Bank Corporation', 17000.00),
('Employee 20', 'First Bank Corporation', 17000.00),
('Employee 21', 'First Bank Corporation', 17000.00),
('Employee 22', 'First Bank Corporation', 17000.00);
DROP FUNCTION IF EXISTS avg_salary;
DELIMITER $$
CREATE FUNCTION IF NOT EXISTS avg_salary (c_name varchar(100))
RETURNS numeric(8,2)
BEGIN
DECLARE a_salary numeric(8,2);
SELECT avg(salary) INTO a_salary FROM works WHERE `company-name` = c_name GROUP BY `company-name`;
RETURN a_salary;
END$$
DELIMITER ;
SELECT
`company-name`
FROM
works
WHERE
avg_salary(`company-name`) < avg_salary('First Bank Corporation')
GROUP BY
`company-name`
It works perfectly for me, returning Company 01 and Company 02 as results.
ADDED after comment by original poster. Query now returns Avg Salary, as well as Company Name,
SELECT
`company-name` AS `Company Name`,
avg_salary(`company-name`) AS `AVG Salary`
FROM
works
WHERE
avg_salary(`company-name`) < avg_salary('First Bank Corporation')
GROUP BY
`company-name`

try something like below:
create function avg_salary (c_name varchar(30))
returns numeric(18,6)
begin
declare a_salary numeric(18,6);
set a_salary = select avg(salary) from works where company_name = c_name;
return (a_salary);
end
select company_name from works where a_salary<(select avg_salary('First Bank Corporation'));

Related

error code: 1046 no database selected when creating database in MySQL

I am getting an error message "error code: 1046 no database selected" using MySQL
Below is the script that I wrote, Any suggestions on why I'm getting the error message?
DROP DATABASE IF EXISTS Bookstore;
Create Database Bookstore;
Use Bookstore;
Create Table Subject
(SubjectCode nvarchar(3) Not Null primary key,
Subject nvarchar(15) Null);
Create Table Book
(ISBN nvarchar(13) Not Null primary key,
Title nvarchar(50) Null,
Author nvarchar(30) Null,
Publisher nvarchar(30) Null,
Subject_Code nvarchar(3) Null References Subject (Subject_Code),
Shelf_Location nvarchar(7) Null,
Fiction bit Null);
Use Bookstore;
Insert Into Subject
Values
('ART', 'Art'),
('BSN', 'Business'),
('BSS', 'Best Seller'),
('EDC', 'Education'),
('FNT', 'Fantasy'),
('HMR', 'Humor'),
('MST', 'Myster'),
('PHL', 'Philosophy'),
('RLG', 'Religion'),
('RMN', 'Romance'),
('SCF', 'Science Fiction'),
('SLH', 'Self Help');
Use Bookstore;
Insert Into Book
Values
('0-111-11111-1', '89 Years in a Sand Trapxx', 'Beck, Fred', 'Hill and Wang', 'HMR', 'RC-1111', '0'),
('0-15-500139-6', 'Business Programming in C', 'Milsspaugh, A.C.', 'The Dryden Press', 'BSN', 'RC-1111', '0'),
('0-394-75843-9', 'Cultural Literacy', 'Hirsch, E.D. Jr.', 'Vintage', 'Bss', 'RC-1115', '0'),
('0-440-22284-2', 'Five Days in Paris', 'Steel, Daniels', 'Dell Publishing', 'RMN', 'RC-1114', '0'),
('0-446-51251-6', 'Megatrends', 'Naisbitt, John', 'Warner Books', 'PHL', 'RC-1114', '0'),
('0-446-51652-X', 'Bridges of Madison County', 'Waller, Robert James', 'Warner Books', 'BSS', 'RC-1114', '1'),
('0-446-60274-4', 'The Rules', 'Fein/Schneider', 'Warner Books', 'SLH', 'RC-1111', '0'),
('0-451-16095-9', 'The Stand', 'King, Stephen', 'Signet', 'MST', 'RC-1113', '0'),
('0-452-26011-6', 'Song of Solomon', 'Morrision, Toni', 'Plume/Penguin', 'BSS', 'Rc-1114', '1'),
('0-517-59905-8', 'How to Talk to Anyone, Anytime, Anywhere', 'King, Larry', 'Crown', 'SLH', 'RC-1113', '0'),
('0-534-26076-4', 'A Quick Guide to the Internet', 'Filus, Steve', 'Intergrated Media Group', 'BSN', 'RC-1111', '0'),
('0-553-24484-X', 'Prospering Woman', 'Ross, Ruth', 'Bantam Brooks', 'SLH', 'RC-1111', '0'),
('0-670-85332-1', 'How to be Hap-Hap-Happy Like Me', 'Markoe, Merrill', 'Viking', 'HMR', 'RC-1113', '1'),
('0-671-67158-8', 'Time Wars', 'Rifkin, Jeremy', 'Simon and Schuster', 'PHL', 'RC-1115', '0'),
('0-697-12897-0', 'Quick Basic and QBasic Using Modular Structure', 'Filus, Steve', 'B & E Tech', 'BSN', 'RC-1112', '0'),
('0-697-21361-7', 'Desktop Publishing Using PageMaker 5.0', 'Filus, Steve', 'B & E Tech', 'BSN', 'RC-1111', '0'),
('0-8007-1213-7', 'Secrets of Closing the Sale', 'Ziglar, Zig', 'Revel', 'BSN', 'RC-1112', '0'),
('0-8041-0753-X', 'The Kitchen God''s Wife', 'Tan, Amy', 'Ivy Books', 'BSS', 'RC-1113', '1'),
('0-8109-3158-3', 'Thomas Cole', 'Powell, Earl A.', 'Abrams', 'ART', 'RC-1112', '0'),
('0-8109-8052-5', 'The Art of Walt Disney', 'Finch, Christopher', 'Albradale', 'Art', 'RC-1112', '0'),
('0-8487-0504', 'Erica Wilson''s Quilts of America', 'Wilson, Erica', 'Oxmoor House', 'ART', 'RC-1112', '0'),
('0-87826-2', 'Know Your Lhaso Apso', 'Schneider, Earl', 'The Pet Library LTD', 'SLH', 'RC-1112', '0'),
('0-89997=087-7', 'Afoot and Afield in Orange County', 'Schad, Jerry', 'Wilderness Press', 'SLH', 'RC-1112', '0'),
('0-915391-40-6', 'Designing User Interfaces', 'Powell, James E.', 'Micontrend', 'BSN', 'RC-1114', '0'),
('0-917849-25-6', 'I am enough', 'Stortz, Margaret', 'Science of Mind', 'PHL', 'RC-115', '0'),
('0-934136-27-0', 'Wine Makers Guide', 'Nury/Fugelsang', 'Western Tanager', 'MST', 'RC-1112', '0'),
('0-9616878-6-X', 'Classics, US Aircraft of World War II', 'Meyer, Mark', 'Howell Press', 'ART', 'RC-1112', '0'),
('1-55615-484-4', 'Code Complete', 'Mc Connell, Steve', 'Microsoft Press', 'BSN', 'RC-1115', '0'),
('1-82423-2218-3', 'The Way', 'Chaney, Elana', 'Tyndale', 'RLG', 'RC-1111', '0');
make sure the database is created with command:
Create Database dbname

Conversion failed when converting date and / or time when creating a table

So I'am making a table with a char, varchar and date. I got an error message saying "Conversion failed when converting date and / or time". If anyone can help me fix, this you got sincere thank you. :D
this is my code for creating and inserting data on my table:
Create table Employee
(EMP_NUM char(3),
EMP_LNAME varchar(15),
EMP_FNAME varchar(15),
EMP_INITIAL char(1),
EMP_HIREDATE date,
JOB_CODE char (3),
EMP_YEARS char(2))
Insert into Employee (EMP_NUM, EMP_LNAME, EMP_FNAME, EMP_INITIAL,
EMP_HIREDATE, JOB_CODE)
Values (101, 'News', 'John', 'G','08-Nov-00', 502),
(102, 'Senior', 'David', 'H','12-Jul-89', 501),
(103, 'Arbough', 'June', 'E','01-Dec-96', 500),
(104, 'Ramoras', 'Anne', 'K','15-Nov-87', 501),
(105, 'Johnson', 'Alice', 'k','01-Feb-93', 502),
(106, 'Smithfield', 'William', null, '22-Jun-04', 500),
(107, 'Alonzo', 'Maria', 'D','10-Oct-93', 500),
(108, 'Washington', 'Ralph', 'B', '22-Aug-91',501),
(109, 'Smith', 'Larry', 'W', '18-Jul-97', 501),
(110, 'Olenko', 'Gerald', 'A', '11-Dec-95', 505),
(111, 'Wabash', 'Geoff', 'B', '04-Apr-91', 506),
(112, 'Smithson', 'Darlene', 'M', '23-Oct-94', 507),
(113, 'Joenbrood', 'Delbert', 'K', '15-ov-96', 508),
(114, 'Jones', 'Annelise', null, '20-Aug-93', 508),
(115, 'Bawangi', 'Travis', 'B','25-Jan-92', 501),
(116, 'Pratt', 'Gerald', 'L','05-Mar-97', 510),
(117, 'Williamson', 'Angie', 'M', '19-Jun-96', 509),
(118, 'Frommer', 'james', 'J','04-Jan-05', 510);
and this is the complete message result :
Msg 241, Level 16, State 1, Line 11
Conversion failed when converting date and/or time from character string.
Use CONVERSION for EMP_HIREDATE column for date :
For ex :
SELECT CAST('18-Jul-97' AS DATE)
In your query :
Insert into Employee (EMP_NUM, EMP_LNAME, EMP_FNAME, EMP_INITIAL,
EMP_HIREDATE, JOB_CODE)
SELECT 101, 'News', 'John', 'G',CAST('08-Nov-00' AS DATE), 502

How to Fill a table with data without a stored procedure/function?

The goal is to create and populate a table with MySQL on the server. The table will be populated with 500 randomly selected names, addresses, and emails.
I created a procedure to complete this task, but it seems that my account does not have the permissions necessary to create stored procedures/functions, which is what every example I can find uses.
When I execute the file I get the following error:
ERROR 10444 (42000): Access denied for user 'username'#'localhost' to database 'db_name'
The file does create the table, it just doesn't get populated.
How can I modify my code so that I don't need a stored procedure?
use dbName;
drop table contacts;
create table dbName.contacts(
uid int(4) unsigned auto_increment primary key,
fname varchar(255),
lname varchar(255),
address varchar(255),
st varchar(2),
zipc int(5),
pNum int(10),
email varchar(255)
);
DELIMITER ##
create procedure fill_contacts_table()
begin
DECLARE counter INT;
SET counter = 0;
while counter < 500 do
insert into contacts(
fname,
lname,
address,
st,
zipc,
pNum,
email)
VALUES(
ELT(1 + FLOOR(RAND()*100),
'Willodean',
'Florentino',
'Gale',
'Tasia',
'Bruno',
'Shanice',
'Chun',
'Moses',
'Fritz',
'Lincoln',
'Jeremy',
'Millie',
'Lorita',
'Quinn',
'Tennie',
'Gretchen',
'Mallory',
'Adolph',
'Jolene',
'Margarete',
'Charmaine',
'Pandora',
'Stephnie',
'Zelma',
'Vena',
'Joan',
'Berniece',
'Leonida',
'Sunni',
'Tonda',
'Keena',
'Evalyn',
'Kaylene',
'Rubie',
'Elia',
'Lorrine',
'Stasia',
'Loura',
'Rosie',
'Fredricka',
'Shawn',
'Felipa',
'Carmel',
'Kay',
'Ashanti',
'Helga',
'Charlotte',
'Hisako',
'Cheyenne ',
'Rudy',
'Pearline',
'Isabelle',
'Hillary',
'Janett',
'Freddy',
'Kristyn',
'Karine',
'Delana',
'George',
'Marianela',
'Laurine',
'Clarissa',
'Han',
'Genny',
'Melita',
'Donette',
'Callie',
'Lavelle',
'Sharonda',
'Kirsten',
'Joanna',
'Muriel',
'Sherwood',
'Rebbeca',
'Jonah',
'Ellie',
'Marita',
'Fumiko',
'Gretchen',
'Stanford',
'Cecilia',
'Audry',
'Jermaine',
'Stephane',
'Cathleen',
'Neida',
'Reynaldo',
'Evelin',
'Irving',
'Sherril',
'Prudence',
'Karri',
'Berta',
'Valeri',
'Alyson',
'Tommy',
'Reina',
'Caren',
'Yon',
'Tereasa'),
ELT(1 + FLOOR(RAND()*100),
'Ress',
'Dusek',
'Eilers',
'Brasch',
'Ascencio',
'Marti',
'Glaze',
'Pardon',
'Alleman',
'Cichon',
'Weaver',
'Clifford',
'Herring',
'Shoe',
'Rey',
'Mccay',
'Mohl',
'Mayo',
'Romberger',
'Dutta',
'Tindal',
'Clarkson',
'Hellyer',
'Albaugh',
'Barfield',
'Bazan',
'Doubleday',
'Freudenthal',
'Motton',
'Bertram',
'Wisneski',
'Luongo',
'Maddocks',
'Mcwilliam',
'Debord',
'Goldner',
'Barham',
'Cogley',
'Maria',
'Harms',
'Parmer',
'Irion',
'Carmean',
'Nilson',
'Corrao',
'Giesler',
'Winkel',
'Jepson',
'Kean',
'Viens',
'Rhett',
'Feather',
'Sonnier',
'Newland',
'Lachermeier',
'Counter',
'Scharf',
'Barrentine',
'Reising',
'Kenny',
'Jett',
'Deshong',
'Amico',
'Kieser',
'Kinsey',
'Yun',
'Rohloff',
'Farinas',
'Abshire',
'Jefferson',
'Forbis',
'Fortune',
'Osbourn',
'Siguenza',
'Davilla',
'Mcelhannon',
'Taniguchi',
'Spiegel',
'Karst',
'Polito',
'Montijo',
'Ager',
'Gee',
'Clutter',
'Stgeorge',
'Masterson',
'Inglis',
'Riemann',
'Auxier',
'Charest',
'Westbrook',
'Bensen',
'Kotek',
'Mullens',
'Chatterton',
'Oommen',
'Delia',
'Jack',
'Zahm',
'Pfau'),
ELT(1 + FLOOR(RAND()*100),
'104 Green Court',
'195 Central St.',
'93 Race Road',
'643 Penn St.',
'75 Country Club Ave.',
'328 North Warren Drive',
'29 Bridgeton Street',
'981 Virginia St.',
'47 Snake Hill Ave.',
'9784 Broad St.',
'279 Livingston Drive',
'555 Orange Street',
'37 Summer Dr.',
'95 Prairie St.',
'37 Fairground St.',
'965 Heather Avenue',
'290 Division St.',
'372 Plumb Branch St.',
'690 Ramblewood St.',
'40 Fremont Court',
'337C Alton Court',
'315 Alderwood St.',
'47 Mill Pond Drive',
'9815 Santa Clara Court',
'9 Fulton Ave.',
'960 Bradford Rd.',
'61 Peg Shop Dr.',
'810 South Dr.',
'6 NE. Cardinal Dr.',
'7438 Bishop Street',
'631 North Gulf Ave.',
'94 Clark St.',
'60 Williams Dr.',
'342 High Ridge Drive',
'9703 Gonzales Lane',
'9127 Alton Road',
'97 Maiden Drive',
'387 Lafayette Avenue',
'8688 Glen Eagles Avenue',
'7057 Sherman Street',
'7813 Pierce St.',
'83 Woodsman Lane',
'7238 West Laurel Ave.',
'553 Tunnel Court',
'8825 Del Monte St.',
'984 East Orchard Drive',
'88 Princess St.',
'40 E. Wakehurst Lane',
'8893 Glenlake Dr.',
'4 School Rd.',
'7239 SE. Thompson Lane',
'627 Warren Ave.',
'595 Purple Finch Street',
'8194 Valley View St.',
'259B Randall Mill Ave.',
'9460 Spring St.',
'9810 Henry Street',
'203 E. Eagle Circle',
'9582 South Charles Lane',
'40 Brewery Ave.',
'19 Fulton St.',
'340 Edgewater St.',
'330 Golden Star Drive',
'616 Lexington Ave.',
'2 S. Peg Shop St.',
'8845 Sutor Rd.',
'9919 Southampton Ave.',
'50 E. University Ave.',
'200 Ryan Ave.',
'38 Walnutwood Road',
'81 East Victoria Dr.',
'6 S. Rocky River Rd.',
'549 Princeton Court',
'9777 Sherman St.',
'7073 Main Street',
'406 Queen Dr.',
'874 Squaw Creek St.',
'9 W. Rose St.',
'27 6th Ave.',
'135 Cobblestone St.',
'531 La Sierra Avenue',
'253 East Fawn Ave.',
'17 SW. Hillside St.',
'5 Tower St.',
'69 Smith Court',
'395 Foster St.',
'20 Race Lane',
'9073 Wagon Dr.',
'20 Fawn Dr.',
'195 Central St.',
'14 S. Cambridge St.',
'535 Griffin Street',
'643 Penn Street',
'19 Cemetery Dr.',
'75 Country Club Ave.',
'439 Military Rd.',
'285 Philmont Court',
'405 Valley View St.',
'8662 Magnolia Dr.',
'7781 Country Club St.'),
ELT(1 + FLOOR(RAND()*51),
'AL',
'AK',
'AZ',
'AR',
'CA',
'CO',
'CT',
'DE',
'FL',
'GA',
'HI',
'ID',
'IL',
'IN',
'IA',
'KS',
'KY',
'LA',
'ME',
'MD',
'MA',
'MI',
'MN',
'MS',
'MO',
'MT',
'NE',
'NV',
'NH',
'NJ',
'NM',
'NY',
'NC',
'ND',
'OH',
'OK',
'OR',
'PA',
'RI',
'SC',
'SD',
'TN',
'TX',
'UT',
'VT',
'VA',
'WA',
'WV',
'WI',
'WY',
'DC'),
ELT(1 + FLOOR(RAND()*100),
'11720',
'44133',
'39564',
'07103',
'41018',
'45211',
'34711',
'33904',
'08021',
'45040',
'12401',
'20715',
'45103',
'19038',
'32303',
'32958',
'19468',
'85302',
'37076',
'48021',
'43512',
'08060',
'15206',
'07840',
'33139',
'30040',
'27529',
'48101',
'85224',
'71730',
'53066',
'19454',
'02072',
'02127',
'32904',
'44145',
'83651',
'30126',
'11572',
'60101',
'20874',
'20707',
'68107',
'30294',
'11552',
'48310',
'07080',
'32955',
'01845',
'02155',
'04106',
'08540',
'06484',
'60435',
'45066',
'32707',
'16601',
'21771',
'52501',
'22304',
'60451',
'60466',
'56401',
'08857',
'50310',
'55316',
'21009',
'46304',
'02131',
'29646',
'37849',
'33702',
'48178',
'28655',
'08520',
'53051',
'74820',
'11417',
'55343',
'13501',
'02148',
'22041',
'34787',
'18015',
'11701',
'48067',
'32003',
'32250',
'07666',
'01801',
'26508',
'06492',
'37072',
'44266',
'20832',
'55330',
'20705',
'60133',
'37388',
'30188'),
ELT(1 + FLOOR(RAND()*100),
'5326650789',
'5458797497',
'3969507190',
'7899779099',
'5737791024',
'7152078410',
'1585609748',
'9114083701',
'3319518922',
'4097559835',
'7795824751',
'8152827507',
'4932036403',
'6402549987',
'4342372370',
'1818285402',
'7764560114',
'5052529831',
'5638654168',
'5533069282',
'4588207833',
'9772641803',
'2105777477',
'5487138355',
'8157763224',
'6076582385',
'3091969824',
'4599611212',
'1622356799',
'2193172740',
'3802198191',
'8874918768',
'3527695220',
'1468081570',
'8781101437',
'1783794554',
'4147642090',
'6946664816',
'5259058695',
'1915347568',
'7271267139',
'6915931769',
'6009627523',
'9326664619',
'5669870035',
'8502271545',
'9069278491',
'3309903311',
'6764406845',
'9709978941',
'4967657222',
'6681379186',
'6876692503',
'9115377994',
'4152903971',
'3221536656',
'6531287939',
'1333154307',
'1989681996',
'9823062288',
'4941263197',
'8242856770',
'4085259464',
'5644693994',
'2471816666',
'7743998254',
'4384095056',
'8761651796',
'6535832572',
'6411644955',
'9128111230',
'6084608842',
'3385174240',
'2605959727',
'5443426087',
'6625356226',
'4048849246',
'3775164894',
'6278296182',
'3357967793',
'9195838006',
'5634927312',
'7928037269',
'1512947086',
'2287557623',
'1797525964',
'2979326380',
'9379217217',
'8984965224',
'6219840588',
'2308367597',
'6971476470',
'2085510840',
'7069816501',
'2994249472',
'7672890630',
'4708647664',
'9064472436',
'7391029735',
'2793660389'),
ELT(1 + FLOOR(RAND()*500),
'myemail.1#e.mail',
'myemail.2#e.mail',
'myemail.3#e.mail',
'myemail.4#e.mail',
'myemail.5#e.mail',
'myemail.6#e.mail',
'myemail.7#e.mail',
'myemail.8#e.mail',
'myemail.9#e.mail',
'myemail.10#e.mail',
'myemail.11#e.mail',
'myemail.12#e.mail',
'myemail.13#e.mail',
'myemail.14#e.mail',
'myemail.15#e.mail',
'myemail.16#e.mail',
'myemail.17#e.mail',
'myemail.18#e.mail',
'myemail.19#e.mail',
'myemail.20#e.mail',
'myemail.21#e.mail',
'myemail.22#e.mail',
'myemail.23#e.mail',
'myemail.24#e.mail',
'myemail.25#e.mail',
'myemail.26#e.mail',
'myemail.27#e.mail',
'myemail.28#e.mail',
'myemail.29#e.mail',
'myemail.30#e.mail',
'myemail.31#e.mail',
'myemail.32#e.mail',
'myemail.33#e.mail',
'myemail.34#e.mail',
'myemail.35#e.mail',
'myemail.36#e.mail',
'myemail.37#e.mail',
'myemail.38#e.mail',
'myemail.39#e.mail',
'myemail.40#e.mail',
'myemail.41#e.mail',
'myemail.42#e.mail',
'myemail.43#e.mail',
'myemail.44#e.mail',
'myemail.45#e.mail',
'myemail.46#e.mail',
'myemail.47#e.mail',
'myemail.48#e.mail',
'myemail.49#e.mail',
'myemail.50#e.mail',
'myemail.51#e.mail',
'myemail.52#e.mail',
'myemail.53#e.mail',
'myemail.54#e.mail',
'myemail.55#e.mail',
'myemail.56#e.mail',
'myemail.57#e.mail',
'myemail.58#e.mail',
'myemail.59#e.mail',
'myemail.60#e.mail',
'myemail.61#e.mail',
'myemail.62#e.mail',
'myemail.63#e.mail',
'myemail.64#e.mail',
'myemail.65#e.mail',
'myemail.66#e.mail',
'myemail.67#e.mail',
'myemail.69#e.mail',
'myemail.69#e.mail',
'myemail.70#e.mail',
'myemail.71#e.mail',
'myemail.72#e.mail',
'myemail.73#e.mail',
'myemail.74#e.mail',
'myemail.75#e.mail',
'myemail.76#e.mail',
'myemail.77#e.mail',
'myemail.78#e.mail',
'myemail.79#e.mail',
'myemail.80#e.mail',
'myemail.81#e.mail',
'myemail.82#e.mail',
'myemail.83#e.mail',
'myemail.84#e.mail',
'myemail.85#e.mail',
'myemail.86#e.mail',
'myemail.87#e.mail',
'myemail.88#e.mail',
'myemail.89#e.mail',
'myemail.90#e.mail',
'myemail.91#e.mail',
'myemail.92#e.mail',
'myemail.93#e.mail',
'myemail.94#e.mail',
'myemail.95#e.mail',
'myemail.96#e.mail',
'myemail.97#e.mail',
'myemail.98#e.mail',
'myemail.99#e.mail',
'myemail.100#e.mail',
'myemail.101#e.mail',
'myemail.102#e.mail',
'myemail.103#e.mail',
'myemail.104#e.mail',
'myemail.105#e.mail',
'myemail.106#e.mail',
'myemail.107#e.mail',
'myemail.108#e.mail',
'myemail.109#e.mail',
'myemail.110#e.mail',
'myemail.111#e.mail',
'myemail.112#e.mail',
'myemail.113#e.mail',
'myemail.114#e.mail',
'myemail.115#e.mail',
'myemail.116#e.mail',
'myemail.117#e.mail',
'myemail.118#e.mail',
'myemail.119#e.mail',
'myemail.120#e.mail',
'myemail.121#e.mail',
'myemail.122#e.mail',
'myemail.123#e.mail',
'myemail.124#e.mail',
'myemail.125#e.mail',
'myemail.126#e.mail',
'myemail.127#e.mail',
'myemail.128#e.mail',
'myemail.129#e.mail',
'myemail.130#e.mail',
'myemail.131#e.mail',
'myemail.132#e.mail',
'myemail.133#e.mail',
'myemail.134#e.mail',
'myemail.135#e.mail',
'myemail.136#e.mail',
'myemail.137#e.mail',
'myemail.138#e.mail',
'myemail.139#e.mail',
'myemail.140#e.mail',
'myemail.141#e.mail',
'myemail.142#e.mail',
'myemail.143#e.mail',
'myemail.144#e.mail',
'myemail.145#e.mail',
'myemail.146#e.mail',
'myemail.147#e.mail',
'myemail.148#e.mail',
'myemail.149#e.mail',
'myemail.150#e.mail',
'myemail.151#e.mail',
'myemail.152#e.mail',
'myemail.153#e.mail',
'myemail.154#e.mail',
'myemail.155#e.mail',
'myemail.156#e.mail',
'myemail.157#e.mail',
'myemail.158#e.mail',
'myemail.159#e.mail',
'myemail.160#e.mail',
'myemail.161#e.mail',
'myemail.162#e.mail',
'myemail.163#e.mail',
'myemail.164#e.mail',
'myemail.165#e.mail',
'myemail.166#e.mail',
'myemail.167#e.mail',
'myemail.168#e.mail',
'myemail.169#e.mail',
'myemail.170#e.mail',
'myemail.171#e.mail',
'myemail.172#e.mail',
'myemail.173#e.mail',
'myemail.174#e.mail',
'myemail.175#e.mail',
'myemail.176#e.mail',
'myemail.177#e.mail',
'myemail.178#e.mail',
'myemail.179#e.mail',
'myemail.180#e.mail',
'myemail.181#e.mail',
'myemail.182#e.mail',
'myemail.183#e.mail',
'myemail.184#e.mail',
'myemail.185#e.mail',
'myemail.186#e.mail',
'myemail.187#e.mail',
'myemail.188#e.mail',
'myemail.189#e.mail',
'myemail.190#e.mail',
'myemail.191#e.mail',
'myemail.192#e.mail',
'myemail.193#e.mail',
'myemail.194#e.mail',
'myemail.195#e.mail',
'myemail.196#e.mail',
'myemail.197#e.mail',
'myemail.198#e.mail',
'myemail.199#e.mail',
'myemail.200#e.mail',
'myemail.201#e.mail',
'myemail.202#e.mail',
'myemail.203#e.mail',
'myemail.204#e.mail',
'myemail.205#e.mail',
'myemail.206#e.mail',
'myemail.207#e.mail',
'myemail.208#e.mail',
'myemail.209#e.mail',
'myemail.210#e.mail',
'myemail.211#e.mail',
'myemail.212#e.mail',
'myemail.213#e.mail',
'myemail.214#e.mail',
'myemail.215#e.mail',
'myemail.216#e.mail',
'myemail.217#e.mail',
'myemail.218#e.mail',
'myemail.219#e.mail',
'myemail.220#e.mail',
'myemail.221#e.mail',
'myemail.222#e.mail',
'myemail.223#e.mail',
'myemail.224#e.mail',
'myemail.225#e.mail',
'myemail.226#e.mail',
'myemail.227#e.mail',
'myemail.228#e.mail',
'myemail.229#e.mail',
'myemail.230#e.mail',
'myemail.231#e.mail',
'myemail.232#e.mail',
'myemail.233#e.mail',
'myemail.234#e.mail',
'myemail.235#e.mail',
'myemail.236#e.mail',
'myemail.237#e.mail',
'myemail.238#e.mail',
'myemail.239#e.mail',
'myemail.240#e.mail',
'myemail.241#e.mail',
'myemail.242#e.mail',
'myemail.243#e.mail',
'myemail.244#e.mail',
'myemail.245#e.mail',
'myemail.246#e.mail',
'myemail.247#e.mail',
'myemail.248#e.mail',
'myemail.249#e.mail',
'myemail.250#e.mail',
'myemail.251#e.mail',
'myemail.252#e.mail',
'myemail.253#e.mail',
'myemail.254#e.mail',
'myemail.255#e.mail',
'myemail.256#e.mail',
'myemail.257#e.mail',
'myemail.258#e.mail',
'myemail.259#e.mail',
'myemail.260#e.mail',
'myemail.261#e.mail',
'myemail.262#e.mail',
'myemail.263#e.mail',
'myemail.264#e.mail',
'myemail.265#e.mail',
'myemail.266#e.mail',
'myemail.267#e.mail',
'myemail.269#e.mail',
'myemail.269#e.mail',
'myemail.270#e.mail',
'myemail.271#e.mail',
'myemail.272#e.mail',
'myemail.273#e.mail',
'myemail.274#e.mail',
'myemail.275#e.mail',
'myemail.276#e.mail',
'myemail.277#e.mail',
'myemail.278#e.mail',
'myemail.279#e.mail',
'myemail.280#e.mail',
'myemail.281#e.mail',
'myemail.282#e.mail',
'myemail.283#e.mail',
'myemail.284#e.mail',
'myemail.285#e.mail',
'myemail.286#e.mail',
'myemail.287#e.mail',
'myemail.288#e.mail',
'myemail.289#e.mail',
'myemail.290#e.mail',
'myemail.291#e.mail',
'myemail.292#e.mail',
'myemail.293#e.mail',
'myemail.294#e.mail',
'myemail.295#e.mail',
'myemail.296#e.mail',
'myemail.297#e.mail',
'myemail.298#e.mail',
'myemail.299#e.mail',
'myemail.300#e.mail',
'myemail.301#e.mail',
'myemail.302#e.mail',
'myemail.303#e.mail',
'myemail.304#e.mail',
'myemail.305#e.mail',
'myemail.306#e.mail',
'myemail.307#e.mail',
'myemail.308#e.mail',
'myemail.309#e.mail',
'myemail.310#e.mail',
'myemail.311#e.mail',
'myemail.312#e.mail',
'myemail.313#e.mail',
'myemail.314#e.mail',
'myemail.315#e.mail',
'myemail.316#e.mail',
'myemail.317#e.mail',
'myemail.318#e.mail',
'myemail.319#e.mail',
'myemail.320#e.mail',
'myemail.321#e.mail',
'myemail.322#e.mail',
'myemail.323#e.mail',
'myemail.324#e.mail',
'myemail.325#e.mail',
'myemail.326#e.mail',
'myemail.327#e.mail',
'myemail.328#e.mail',
'myemail.329#e.mail',
'myemail.330#e.mail',
'myemail.331#e.mail',
'myemail.332#e.mail',
'myemail.333#e.mail',
'myemail.334#e.mail',
'myemail.335#e.mail',
'myemail.336#e.mail',
'myemail.337#e.mail',
'myemail.338#e.mail',
'myemail.339#e.mail',
'myemail.340#e.mail',
'myemail.341#e.mail',
'myemail.342#e.mail',
'myemail.343#e.mail',
'myemail.344#e.mail',
'myemail.345#e.mail',
'myemail.346#e.mail',
'myemail.347#e.mail',
'myemail.348#e.mail',
'myemail.349#e.mail',
'myemail.350#e.mail',
'myemail.351#e.mail',
'myemail.352#e.mail',
'myemail.353#e.mail',
'myemail.354#e.mail',
'myemail.355#e.mail',
'myemail.356#e.mail',
'myemail.357#e.mail',
'myemail.358#e.mail',
'myemail.359#e.mail',
'myemail.360#e.mail',
'myemail.361#e.mail',
'myemail.362#e.mail',
'myemail.363#e.mail',
'myemail.364#e.mail',
'myemail.365#e.mail',
'myemail.366#e.mail',
'myemail.367#e.mail',
'myemail.369#e.mail',
'myemail.369#e.mail',
'myemail.370#e.mail',
'myemail.371#e.mail',
'myemail.372#e.mail',
'myemail.373#e.mail',
'myemail.374#e.mail',
'myemail.375#e.mail',
'myemail.376#e.mail',
'myemail.377#e.mail',
'myemail.378#e.mail',
'myemail.379#e.mail',
'myemail.380#e.mail',
'myemail.381#e.mail',
'myemail.382#e.mail',
'myemail.383#e.mail',
'myemail.384#e.mail',
'myemail.385#e.mail',
'myemail.386#e.mail',
'myemail.387#e.mail',
'myemail.388#e.mail',
'myemail.389#e.mail',
'myemail.390#e.mail',
'myemail.391#e.mail',
'myemail.392#e.mail',
'myemail.393#e.mail',
'myemail.394#e.mail',
'myemail.395#e.mail',
'myemail.396#e.mail',
'myemail.397#e.mail',
'myemail.398#e.mail',
'myemail.399#e.mail',
'myemail.400#e.mail',
'myemail.401#e.mail',
'myemail.402#e.mail',
'myemail.403#e.mail',
'myemail.404#e.mail',
'myemail.405#e.mail',
'myemail.406#e.mail',
'myemail.407#e.mail',
'myemail.408#e.mail',
'myemail.409#e.mail',
'myemail.410#e.mail',
'myemail.411#e.mail',
'myemail.412#e.mail',
'myemail.413#e.mail',
'myemail.414#e.mail',
'myemail.415#e.mail',
'myemail.416#e.mail',
'myemail.417#e.mail',
'myemail.418#e.mail',
'myemail.419#e.mail',
'myemail.420#e.mail',
'myemail.421#e.mail',
'myemail.422#e.mail',
'myemail.423#e.mail',
'myemail.424#e.mail',
'myemail.425#e.mail',
'myemail.426#e.mail',
'myemail.427#e.mail',
'myemail.428#e.mail',
'myemail.429#e.mail',
'myemail.430#e.mail',
'myemail.431#e.mail',
'myemail.432#e.mail',
'myemail.433#e.mail',
'myemail.434#e.mail',
'myemail.435#e.mail',
'myemail.436#e.mail',
'myemail.437#e.mail',
'myemail.438#e.mail',
'myemail.439#e.mail',
'myemail.440#e.mail',
'myemail.441#e.mail',
'myemail.442#e.mail',
'myemail.443#e.mail',
'myemail.444#e.mail',
'myemail.445#e.mail',
'myemail.446#e.mail',
'myemail.447#e.mail',
'myemail.448#e.mail',
'myemail.449#e.mail',
'myemail.450#e.mail',
'myemail.451#e.mail',
'myemail.452#e.mail',
'myemail.453#e.mail',
'myemail.454#e.mail',
'myemail.455#e.mail',
'myemail.456#e.mail',
'myemail.457#e.mail',
'myemail.458#e.mail',
'myemail.459#e.mail',
'myemail.460#e.mail',
'myemail.461#e.mail',
'myemail.462#e.mail',
'myemail.463#e.mail',
'myemail.464#e.mail',
'myemail.465#e.mail',
'myemail.466#e.mail',
'myemail.467#e.mail',
'myemail.469#e.mail',
'myemail.469#e.mail',
'myemail.470#e.mail',
'myemail.471#e.mail',
'myemail.472#e.mail',
'myemail.473#e.mail',
'myemail.474#e.mail',
'myemail.475#e.mail',
'myemail.476#e.mail',
'myemail.477#e.mail',
'myemail.478#e.mail',
'myemail.479#e.mail',
'myemail.480#e.mail',
'myemail.481#e.mail',
'myemail.482#e.mail',
'myemail.483#e.mail',
'myemail.484#e.mail',
'myemail.485#e.mail',
'myemail.486#e.mail',
'myemail.487#e.mail',
'myemail.488#e.mail',
'myemail.489#e.mail',
'myemail.490#e.mail',
'myemail.491#e.mail',
'myemail.492#e.mail',
'myemail.493#e.mail',
'myemail.494#e.mail',
'myemail.495#e.mail',
'myemail.496#e.mail',
'myemail.497#e.mail',
'myemail.498#e.mail',
'myemail.499#e.mail',
'myemail.500#e.mail')
);
SET counter = counter + 1;
end while;
end##
DELIMITER ;
See the answer to this SO question by #pupitetris to create a sequence table. Replace your VALUES clause with a SELECT clause that selects FROM that sequence table, using the expressions that you already have to select random fname, lname, etc.

Getting data if one value is NULL

I am having an issue with the code below and my issue is the following:
I need to be able to select the shop_shipping_rule_region_code if the country_iso is NULL but I also need to get the $country_iso data as well if present
What I am after:
The Final Result will have:
Overnight UPS
NZ Snail Mail
Whole World (in this example)
DB:
`shop_shipping_rules` (`shop_shipping_rule_id`, `shop_shipping_rule_country_iso`, `shop_shipping_rule_region_code`, `shop_shipping_rule_name`,
`shop_shipping_rule_type_single`, `shop_shipping_rule_item_single`, `shop_shipping_rule_type_multi`,
`shop_shipping_rule_item_multiple`,`shop_shipping_rule_created`, `shop_shipping_rule_modified`, `website_id`)
VALUES
(41, 'NZ', 'ALL', 'Overnight UPS', 'single', 2.00, 'multi', 4.00, '2013-11-05 02:30:19', '2013-11-05 03:00:27', 64),
(44, 'NZ', NULL, 'NZ Snail Mail', 'single', 25.00, 'multi', 35.00, '2013-11-14 03:57:06', NULL, 64),
(45, NULL, 'ALL', 'Whole World', 'single', 90.00, 'multi', 150.00, '2013-11-14 05:05:53', NULL, 64),
(46, NULL, 'EU', 'EU Ship', 'single', 28.00, 'multi', 35.00, '2013-11-15 01:04:01', NULL, 64);
if (isset($country_iso))
$sql = "SELECT * FROM shop_shipping_rules WHERE country_iso IS NULL OR country_iso = '$country_iso' ";
else
$sql = "SELECT * FROM shop_shipping_rules WHERE country_iso IS NULL";

Updating the database from 1.5.1.3 to 1.5.3.1 OpenCart

I'm trying to pass data from my table OpenCart version 1.5.1.3 to version 1.5.3.1. I'll be using a new theme and only a few of the modules used in the previous version, but do not want to lose the data records of customers, sales and products.
Bearing this in mind I guess the easiest way would be estrair the table to a SQL script (I used phpmyadmin), but there are some differences between the versions for the database schema and data so I am not able to pass data from a table another.
As an example I'll use the table "address" 1.5.1.3 version that has the following structure when exported by phpmyadmin:
INSERT INTO `address` (`address_id`, `customer_id`, `firstname`, `lastname`, `company`, `address_1`, `address_2`, `city`, `postcode`, `country_id`, `zone_id`) VALUES
(6, 6, 'Fulano', 'Silva', '', 'My Street, 455', 'Neighborhood 1', 'City 1', 'd9c 5t7', 30, 464),
(2, 2, 'Cicrano', 'Souza', '', 'My Avenue, 921', 'Neighborhood 2', 'City 2', 'd9c 5t7', 30, 464),
(4, 4, 'Beltrano', 'Cabrito', '', 'My Street 2, 191', 'Neighborhood 3', 'City 3', 'd9c 3t7', 30, 464);
And the database version 1.5.3.1 follows this model:
INSERT INTO `address` (`address_id`, `customer_id`, `firstname`, `lastname`, `apelido`, `company`, `company_id`, `tax_id`, `address_1`, `numero`, `address_2`, `complemento`, `city`, `postcode`, `country_id`, `zone_id`) VALUES
(6, 6, 'Fulano', 'Silva', '', '', '', '', 'My Street', '455', 'Neighborhood 1', '', 'City 1', 'd9c 5t7', 30, 464),
(2, 2, 'Beltrano', 'Cabrito', '', '', '', '', '', 'My Street 2', '191', 'Neighborhood 3', '', 'City 3', 'd9c 3t7', 30, 464);
This is repeated in several tables, now how do I get only the data you want from v1.5.1.3 to v1.5.3.1 using SQL or other simpler if any?
This is why the upgrade script is there. You can find out how to upgrade here