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');
Related
Trying to create a new WordPress user directly through MySQL by running a query. I keep seeing an error message popup :
1292 - Incorrect datetime value: '0000-00-00 00:00:00' for column 'user_registered' at row 1
How can include what is missing?
INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`)
VALUES ('adminuser2', MD5('adminuser2password'), 'adminfirstname adminlastname', 'adminuser2#mywebsite.com', '0');
INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (Select max(id) FROM wp_users), 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');
INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (Select max(id) FROM wp_users), 'wp_user_level', '10');
Follow this syntax, it's the recommended way to insert data while you're using WordPress
$wpdb->insert( $table_name, array('column_name_1'=>'hello', 'other'=> 123), array( '%s', '%d' ) );
define a variable:
$blogtime = current_time( 'mysql' ); then add 'user_registered' column to your query
$wpdb->insert( 'wp_users' , array(
'user_login' => 'adminuser2',
'user_pass' => MD5('adminuser2password'),
'user_nicename'=> 'adminfirstname adminlastname',
'user_email'=> 'adminuser2#mywebsite.com',
'user_status'=> '0',
'user_registered'=> '$blogtime'
));
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.
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 8 years ago.
Thank you for your comments on this error
INSERT INTO donors (
'institution',
'contact_person',
'first_name',
'last_name',
'affiliation',
'picture_link',
'email',
'birthday',
'sex',
'street',
'city',
'state',
'zip'
)
VALUES (
'',
'',
'Claire',
'Bowens',
'employee',
'http://las-americas.org/wp-content/uploads/2010/10/Claire-staff-photo.jpg',
'#',
'yyyy-mm-dd',
'female',
'',
'El Paso',
'Texas',
''
)
Error: 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 ''institution', 'contact_person', 'first_name', 'last_name', 'affiliation', 'p' at line 1
To enclose identifiers use backticks => `` `
In your code you used '.
INSERT INTO donors ( `institution`, `contact_person`, `first_name`, `last_name`, `affiliation`?, `picture_link`, `email`, `birthday`, `sex`, `street`, `city`, `state`, `zip`) VALUES ('', '', 'Claire', 'Bowens', 'employee', 'http://las-americas.org/wp-content/uploads/2010/10/Claire-staff-photo.jpg', '#', 'yyyy-mm-dd', 'female', '', 'El Paso', 'Texas', '' )
(Single) Quotes are just used for string literals.
A more detailed answer can be found in this question:
https://stackoverflow.com/a/11321508/1169798
remove the ' from your table fields like this.
INSERT INTO donors (
institution,
contact_person,
first_name,
last_name,
affiliation,
picture_link,
email,
birthday,
sex,
street,
city,
state,
zip
)
VALUES (
'',
'',
'Claire',
'Bowens',
'employee',
'http://las-americas.org/wp-content/uploads/2010/10/Claire-staff-photo.jpg',
'#',
'yyyy-mm-dd',
'female',
'',
'El Paso',
'Texas',
''
)
Here is the scenario:
UPDATED
Thanks to anakata for pointing out table locks and problems with overlapping sessions.
Start Transaction
Run a bunch of queries, these 2 in particular (private data removed):
START TRANSACTION
... various select statements...
INSERT INTO `leads` (`vertical_id`, `offer_id`, `campaign_id`, `affiliate_id`, `creative_id`, `is_test`, `ignoreme`, `qualified`, `usid`, `first_name`, `last_name`, `email`, `address`, `city`, `state`, `zip`, `phone`, `ip_address`, `referrer`, `duplicate_hash`, `address2`, `form_message`, `microtime`, `created`, `sub_id`, `score`, `imported`, `buyer_price`, `purchased_price`) VALUES ('45', '82', '151', '3', '0', 0, 0, 1, '0510', 'CharXX', 'MXX', 'XXXXX#yahoo.com', '451 XXX', 'Hagerstown', 'MD', '21740', '301-------', '405877318', 'X', 'e0534ea843c35d0f300ed4ff6e65b8db', '', 'redacted', '1368665153.6829', NOW(), '333', 0, 0, '0', '0')
INSERT INTO `fields_leads` (`lead_id`, `field_id`, `vertical_id`, `offer_id`, `campaign_id`, `value`) VALUES ('56575', '866', '45', '82', '151', 'CharXX')
COMMIT
Meanwhile other transactions are taking place. Post trasaction the insertID of 56575 is used as keys in other tables for various business related stuffs.
Problem is when you go to look for leads.id=56575 its gone! There are no DELETE FROM leads statements in the general.log. No rollback statements regarding this query either. I'm running MySQL client version: 5.0.77. Any insight would be super helpful, thanks!
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
;