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;
Related
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
I'm trying to update the database, using a script where the ID of a user isn't readily known, so I'm using a subquery to have mysql find the user id (for the posteruserid value). This is the SQL query i'm using:
INSERT INTO `thread` (`title`, `forumid`, `open`, `replycount`,
`postercount`, `postusername`, `postuserid`, `lastposter`,
`dateline`, `visible`, `keywords`)
SELECT 'IN', 2, 1, 0, 1, 'lemons', `userid` FROM `user`
WHERE `username` = 'lemons', 'lemons', 1375768440, 1, 'IN';
I'm getting a syntax error from the above SQL, and I can't figure out what I'm doing wrong.
EDIT because of the mismatched column name, I tried using an alias, which still doesn't work
INSERT INTO `thread` (`title`, `forumid`, `open`, `replycount`,
`postercount`, `postusername`, `postuserid`, `lastposter`,
`dateline`, `visible`, `keywords`)
SELECT 'IN', 2, 1, 0, 1, 'lemons',
`userid` AS `postuserid` FROM `user` WHERE `username` = 'lemons',
'lemons', 1375768440, 1, 'IN';
column mismatch in insert and select query..column should be same where you are going to insert and from where you are fetching data.
You specify to insert values of 11 columns, but in your SELECT statement, you are providing only 7 values. Please provide the value for lastposter,dateline,visible, and keywords.
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.
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
INSERT INTO `empleado` VALUES ('100', 'Alfonso', '1999-11-22', '100', '11');
INSERT INTO `empleado` VALUES ('101', 'Encarna', '2001-11-12', '100', '15');
INSERT INTO `empleado` VALUES ('102', 'Paco', '1999-10-16', '101', '12');
INSERT INTO `empleado` VALUES ('103', 'Juan Carlos', '1999-01-12', '101', '10');
That's my Date Type ,I need to select some codes that got into the company in the 3rd term of 1999
"select nombre,coddep,fecha_ingreso from
empleado where fecha_ingreso >1999;"
-that was my initial query, but it's wrong since it selects the name,cod and datetime >1999
This will fetch rows in the 3rd calendar quarter of 1999:
SELECT nombre, coddep, fecha_ingreso
FROM empleado
WHERE fecha_ingreso BETWEEN '1999-07-01' AND '1999-09-30'
If the field is DATETIME rather than DATE, change the second date to 1999-09-30 12:59:59
SELECT nombre, coddep, fecha_ingreso
FROM empleado
WHERE YEAR(fecha_ingreso) = 1999 AND QUARTER(fecha_ingreso) = 3
;