Insert row for each userid for fields from other table - mysql

I am learning MySQL and so don't know complex query. So need your expert help.
I have rc_userfields table with 4 fields, key and position which I already have. Now I want to add this 4 fields into rc_profile table. I want to add it for all userid. Means all 4 fields will be added for all userid.
rc_userfields table
Userid can be refereed from rc_users table
rc_users table
So final result would be something like below image
rc_profile table
Please refer below image for all three tables:
I know how can I insert manually with below code:
INSERT INTO `rc_profile` (`userid`, `keys`, `data`) VALUES
(1, 'myself', ''),
(1, 'position', ''),
(1, 'nickname', ''),
(1, 'blog', ''),
(2, 'myself', ''),
(2, 'position', ''),
(2, 'nickname', ''),
(2, 'blog', ''),
(3, 'myself', ''),
(3, 'position', ''),
(3, 'nickname', ''),
(3, 'blog', ''),
(4, 'myself', ''),
(4, 'position', ''),
(4, 'nickname', ''),
(4, 'blog', '');
Finally: I want to add all 4 fields keys from rc_userfields table to rc_profile table for every user in rc_users table

You need get the cartesian product of the two tables using CROSS JOIN and the result will be inserted on table rc_profile, eg.
INSERT INTO rc_profile (userID, `keys`, data)
SELECT a.userID, b.key, '' data
FROM rc_users a CROSS JOIN rc_userfields b
SQLFiddle Demo

Related

My code is only returning two of the five entries I need, how can I fix this problem?

I am trying to create a list of employees showing their first name, hire date, and the city where they work. There are five employees listed but when I run my problem it only returns the first two from the location data entry.
Shown first is my employee data entered. When I run a select * statement for this table everything appears as it should. The problem comes when I try to run the second script shown, where it only shows the first two entries from the employee table.
first
INSERT INTO `salon`.`employee`
(`employee_id`,
`first_name`,
`last_name`,
`title`,
`hire_date`,
`termination_date`,
`email`,
`phone`,
`location_id`,
`manager_id`)
VALUES
(1,
'Jenny',
'Jensen',
'Owner',
'2013-06-15',
NULL,
'jensenj#salon.com',
2081112222,
1,
NULL),
(2,
'Haley',
'Lopez',
'Assistant Manager',
'2013-08-23',
NULL,
'lopezh#salon.com',
2083334444,
1,
1),
(3,
'Robert',
'Green',
'Associate',
'2014-01-03',
NULL,
'greenr#salon.com',
2085556666,
1,
2),
(4,
'Olive',
'Adams',
'Manager',
'2015-07-12',
NULL,
'adamso#salon.com',
2087778888,
2,
1),
(5,
'Julie',
'Davis',
'Associate',
'2015-10-20',
NULL,
'davisj#salon.com',
2089990000,
2,
4);
second script
SELECT employee.first_name, hire_date, location.city
FROM employee
JOIN location ON employee.employee_id = location.location_id

mysql: find newest non null values

I have a huge table with 100 fields. Each row is timestamped. I want to find the latest non null value for all columns. I'm using MySql 5.6 InnoDB
e.g.
create table tester(
pub_id int,
pub_name varchar(20),
pub_city varchar(20),
country varchar(20),
no_of_branch varchar(20),
estd datetime
);
insert into tester (pub_id, pub_name, pub_city, country, estd)
values
(1, 'a', 'xyz', 'abcity' , 'a', '1970-01-01 00:00:01'),
(2, 'a', 'xyz', '' , 'a', '1971-01-01 00:00:01'),
(3, 'a', 'xyz', 'abcity1', 'b', '1972-01-01 00:00:01'),
(4, 'a', 'xyz', '' , 'a', '1973-01-01 00:00:01'),
(5, 'a', 'xyz', 'abcity2', '' , '1974-01-01 00:00:01'),
(6, 'b', 'lmn', 'abcity' , 'a', '1974-01-01 00:00:01'),
(7, 'b', 'xyz', '' , 'a', '1975-01-01 00:00:01'),
(8, 'b', 'sdf', 'abcity1', 'b', '1976-01-01 00:00:01'),
(9, 'b', '' , '' , 'a', '1977-01-01 00:00:01'),
(10, 'b', '' , 'abcity2', '' , '1978-01-01 00:00:01');
I want to query that would give me:
'a', 'xyz', 'abcity2', 'a'
'b', 'sdf', 'abcity2', 'b'
I don't want to use a query where i find empty values for each column of the table individually and then take a join as this would be a very cumbersome task given that my actual table has 100 columns.
I have searched for a solution for the past of couple of ours and found nothing. ANy help would be greatly appreciated.
Thanks
This might be the "tricky" way you are looking for.
First create a twin table (tester2) to receive the aggregated data. This new table must have a primary key on pub_name and all the columns you want to aggregate. Then do an INSERT INTO ON DUPLICATE KEY UPDATE. This query will basically rebuild the tester table but without duplicate and with aggregated data. In fact something like this :
insert into tester2 (pub_name, pub_city, country, no_of_branch)
select pub_name, pub_city, country, no_of_branch FROM tester order by estd desc
on duplicate key
update
pub_city = coalesce(tester2.pub_city,tester.pub_city),
country = coalesce(tester2.country,tester.country),
no_of_branch = coalesce(tester2.no_of_branch,tester.no_of_branch)
The content of tester2 will be :
PUB_NAME PUB_CITY COUNTRY NO_OF_BRANCH
a xyz abcity2 a
b sdf abcity2 a
Have a look the DB Fiddle.
Note : I assume you mean real NULL values and not empty string like the sample you provided.

MySQL. Counting just a part of my column value

I have a question relating tables in MySQL. To understand this better I'd rather show you. I have the following table:
CREATE TABLE IF NOT EXISTS `tip_masina` (
`id_tip` int(11) NOT NULL AUTO_INCREMENT,
`marca` varchar(40) NOT NULL,
`pret` int(11) NOT NULL,
PRIMARY KEY (`id_tip`),
UNIQUE KEY `marca` (`marca`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
INSERT INTO `tip_masina` (`id_tip`, `marca`, `pret`) VALUES
(1, 'Chevrolet Impala', 8000),
(2, 'Chevrolet Camaro', 10000),
(3, 'Chevrolet Tahoe', 13000),
(4, 'Chevrolet Suburban', 12500),
(5, 'Chevrolet Cobalt', 4000),
(6, 'Dodge Charger', 14000),
(7, 'Dodge Avenger', 9000),
(8, 'Dodge Challenger', 6500),
(9, 'Dodge Dart', 3500),
(10, 'Dodge Durango', 3000),
(11, 'Ford Mustang', 7500),
(12, 'Ford Crown Victoria', 5000),
(13, 'Ford Focus', 4300),
(14, 'Ford Fiesta', 3700),
(15, 'Ford Escort', 1000);
What I want out of this table is to display the vehicle type and the number of vehicles, like:
marca | no_of_vehicles
Chevrolet 5
Dodge 5
Ford 5
Is there any way to do this without splitting the column marca in two columns?
Here is an easy way, using substring_index():
select substring_index(marca, ' ', 1) as marca, count(*)
from tip_masina
group by substring_index(marca, ' ', 1);
It might be better to split 'marca' into two columns so that it would be easier to find what you want. You won't need any special functions then (as e.g. substring_index).
EDIT:
You can use following code:
EDIT2:
Now it works (tested on mysql workbench with your queries creating table):
START TRANSACTION;
ALTER TABLE tip_masina ADD model VARCHAR(60) AFTER marca;
ALTER TABLE tip_masina CHANGE marca company VARCHAR(60);
UPDATE tip_masina SET model = SUBSTRING_INDEX(company, ' ', -1);
ALTER TABLE tip_masina DROP INDEX marca;
UPDATE tip_masina SET company = SUBSTRING_INDEX(company, ' ', 1);
SELECT * FROM tip_masina;

INSERT in some tables's Simultaneous in MYSQL

I try to insert my data to some table's of database I have tried the following:
INSERT INTO tbl_18189 (a,b,c,d,e) VALUES ( '1', '4 B', '%', '0', '0')
INSERT INTO tbl_3823 (a,b,c,d,e) VALUES ( '24000', '30 M', '34%', '885', '12.05')
INSERT INTO tbl_67126 (a,b,c,d,e) VALUES ( '3.99 M', '10 B', '10%', '530', '14.41')
INSERT INTO tbl_4247 (a,b,c,d,e) VALUES ( '1', '170 M', '%', '271', '22.77')
INSERT INTO tbl_23838 (a,b,c,d,e) VALUES ( '320000', '400 M', '7%', '407', '6.91')
but I got
Error creating table: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use
near 'INSERT INTO tbl_38232 ...
where is the problem in my code
UNION ALL joins SELECTs and you have none in your example. Remove them.
INSERT INTO tbl_18189 (a,b,c,d,e) VALUES ( '1', '4 B', '%', '0', '0');
INSERT INTO tbl_3823 (a,b,c,d,e) VALUES ( '24000', '30 M', '34%', '885', '12.05');
INSERT INTO tbl_67126 (a,b,c,d,e) VALUES ( '3.99 M', '10 B', '10%', '530', '14.41');
INSERT INTO tbl_4247 (a,b,c,d,e) VALUES ( '1', '170 M', '%', '271', '22.77');
INSERT INTO tbl_23838 (a,b,c,d,e) VALUES ( '320000', '400 M', '7%', '407', '6.91');

Replacing strings in MySQL?

I have those strings in y table:
abcdefg_1056-DF or
123erttzz-1292 or
gdfgdfg_1056
What I want is only the first part, abcdefg for example for the first string. So I can replace all numbers and all -DF with an empty string, but I dont know how.
Ideas?
I would suggest a regex replace. Have a look here - I think this might help you:
How to do a regular expression replace in MySQL?
If you prever a lazy and ugly method (and not recommended for very, very much rows), you can spare yourself a user defined function like in mwerner's answer and do it simply like this:
select
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(asdf, '0', ''), '1', ''), '2', ''), '3', ''), '4', ''), '5', ''), '6', ''), '7', ''), '8', ''), '9', ''), '_', ''), '-DF', '')
from
(
select
'abcdefg_1056-DF' as asdf
union select
'123erttzz-1292'
union select
'gdfgdfg_1056'
)q