sql insert query with select - mysql

Given this table where both reviewid and prankid are auto increment.
CREATE TABLE Review
(
reviewId INT NOT NULL AUTO_INCREMENT,
email VARCHAR(32) NOT NULL,
prankId INT NOT NULL,
rating INT,
comment VARCHAR(1056) NOT NULL,
PRIMARY KEY(reviewId),
FOREIGN KEY (email) REFERENCES User(email),
FOREIGN KEY (prankId) REFERENCES Prank(prankId)
);
Would this insert statement correctly insert values into all of the attributes in review table.
INSERT INTO Review (email, prankId) SELECT email, prankId from User;
INSERT INTO Review (rating, comment) VALUES(‘5’,’amazing!’);
INSERT INTO Review (rating, comment) VALUES(‘5’,’brilliant!’);

I suspect that you want this:
INSERT INTO Review (email, prankId, rating, comment)
SELECT email, prankId, 'S', 'amazing!'
from User;
INSERT INTO Review (email, prankId, rating, comment)
SELECT email, prankId, 'S', 'brilliant'
from User;
In your version, the last two inserts will fail because email and prankid are NULL. Remember, insert adds new rows into the table. It doesn't modify existing rows.

Related

How do you ON DUPLICATE KEY UPDATE a user info without breaking the database?

I have a simple table with students in MySQL database. Similar to:
student_id
student_name
teacher_id
1
Adam
100
2
Bob
100
3
Carl
100
4
Dan
200
Teachers can input new students or change existing student's names. Currently I have this set up to update via:
INSERT INTO student_list (name) VALUES (:name) ON DUPLICATE KEY UPDATE student name= values(name)
The problem is that this completely breaks any associated student_ids in other tables (as the ON DUPLICATE KEY UPDATE changes the student_id). I could look up each entry before updating, but it seems messy. Surely there's a better way.
CREATE TABLE `sabrep_db`.`students` ( `student_id` INT NOT NULL AUTO_INCREMENT , `name` TEXT NOT NULL , `teacher_id` INT NOT NULL , PRIMARY KEY (`student_id`)) ENGINE = InnoDB;
INSERT INTO `students` (`student_id`, `name`, `teacher_id`) VALUES (NULL, 'Adam', '100'), (NULL, 'Bob', '100');
Should also pass a key (usually the primary key) as part of the insert so that it knows the key reference to update when there is a duplicate, name is passed twice for the update portion:
INSERT INTO student_list (student_id, name)
VALUES (:student_id, :name) ON DUPLICATE KEY UPDATE student_name = :name;

How to get the user with most comments from a table

I am trying to get the user first name with the most comments. How can I do this?
Here are the tables.
The tables below are the setup for the database tables which I am trying to query.
CREATE TABLE User(
userid varchar(3),
firstname varchar(20),
lastname varchar(20),
age int,
PRIMARY KEY(userid)
)ENGINE=INNODB;
CREATE TABLE Comment(
commentid varchar(3),
userid varchar(3),
eventid varchar(3),
title varchar(20),
comment varchar(50),
PRIMARY KEY(commentid),
FOREIGN KEY(userid) REFERENCES AnonymousUser(userid),
FOREIGN KEY(eventid) REFERENCES Event(eventid)
)ENGINE=INNODB;
INSERT INTO User VALUES('U01','Charles','Darwin',99);
INSERT INTO User VALUES('U02','Keisha','Strawn',24);
INSERT INTO User VALUES('U03','Denise','Malcolm',59);
INSERT INTO User VALUES('U04','Dennis','Stewart',19);
INSERT INTO User VALUES('U05','Robert','Johns',45);
INSERT INTO User VALUES('U06','Marsha','Stewart',33);
INSERT INTO Comment VALUES ('C01','A01','E01','Boring Event','This event was boring');
INSERT INTO Comment VALUES ('C02','A02','E01','Nice Nice Event','This event was Nice');
INSERT INTO Comment VALUES ('C03','A03','E03','Wow','This event was Amazing');
INSERT INTO Comment VALUES ('C04','A01','E01','Very Sad','I missed this event');
The query I tried is
SELECT User.userid FROM User
JOIN comment ON comment.userid = user.userid
WHERE (SELECT COUNT(comment)
FROM comment = (SELECT MAX(userid) FROM comment);
SELECT
userid
FROM
comment
GROUP BY userid
ORDER BY count(userid) DESC
LIMIT 1;
Edit: oh, you need the username. Try this:
SELECT firstname
FROM user
WHERE userid = (
SELECT
userid
FROM
comment
GROUP BY userid
ORDER BY count(userid) DESC
LIMIT 1
);
Query to get firstname with most comments is
select a.firstname, max(a.comment_count) from (
select u.firstname, count(c.commentid) comment_count
from user u join comment c on u.userid = c.userid
group by u.firstname
)a;
That said, I noticed
One of the constraints on table 'comment' is pointing to a table 'AnonymousUser' FOREIGN KEY(userid) REFERENCES AnonymousUser(userid). You have not shared the create table statement for that table.
I had to remove this constraint from table definition in order to successfully create this table in my database
CREATE TABLE Comment(
commentid varchar(3),
userid varchar(3),
eventid varchar(3),
title varchar(20),
comment varchar(50),
PRIMARY KEY(commentid)
);
You dataset for table 'comment' has no userid matching 'user.userid' values
I updated the 'comment' table inserts so I could get some result when executing my query.
INSERT INTO Comment VALUES ('C01','U01','E01','Boring Event','This event was boring');
INSERT INTO Comment VALUES ('C02','U01','E01','Nice Nice Event','This event was Nice');
INSERT INTO Comment VALUES ('C03','U03','E03','Wow','This event was Amazing');
INSERT INTO Comment VALUES ('C04','U06','E01','Very Sad','I missed this event');

Using AUTO_INCREMENT with other value MYSQL generated, duplicate rows generated. How to replace id with my values?

I am replacing auto_increment id with my own(of sequential and auto generated from a base number). I used ALTER TABLE tablename AUTO_INCREMENT = 701.
MySQL produced duplicates(ones with auto generated from "1" and (another sets from "701. Please see the scripts as below: Kindly help me to avoid MYSQL generated rows. Much obliged.
use login_db;
show databases;
CREATE TABLE logtable
(
id int NOT NULL AUTO_INCREMENT, accountno char(8) not null, PRIMARY KEY (id),
username VARCHAR(20) NOT NULL,
password varchar(20),
firstname varchar(8),
lastname varchar(8),
email varchar(30)
);
alter table logtable AUTO_INCREMENT = 701;
insert into logtable (username,password,firstname,lastname,email) values
('kamitrust','rust8115','john','lee','support#poosung.com');
Select * from logtable;
insert into logtable (username,password,firstname,lastname,email) values ('pinktop','winston19','jeff','kim','aol#poosung.com');
Select * from logtable;
The result from executing the script above:
# id, accountno, username, password, firstname, lastname, email
'1', '', 'kamitrust', 'rust8115', 'john', 'lee', 'support#poosung.com'
'2', '', 'pinktop', 'winston19', 'jeff', 'kim', 'aol#poosung.com'
'701', '', 'kamitrust', 'rust8115', 'john', 'lee', 'support#poosung.com'
'702', '', 'pinktop', 'winston19', 'jeff', 'kim', 'aol#poosung.com'

What is the SQL to do a SELECT statement within an INSERT statement?

I have the following tables in my MySQL database:
CREATE TABLE user_role (
user_role_id INT UNSIGNED AUTO_INCREMENT,
user_role VARCHAR(15) NOT NULL,
CONSTRAINT pk_user_role PRIMARY KEY (user_role_id)
);
CREATE TABLE users (
user_id INT UNSIGNED AUTO_INCREMENT,
user_role_id INT UNSIGNED NOT NULL,
email VARCHAR(254) NOT NULL,
password VARCHAR(255) NOT NULL,
CONSTRAINT pk_users PRIMARY KEY (user_id),
CONSTRAINT fk1_users FOREIGN KEY (user_role_id) REFERENCES user_role (user_role_id),
CONSTRAINT unq1_users UNIQUE (email)
);
Assuming all the user roles already exist, how can I insert a new user with the proper user role id (i.e. SELECT user_role_id WHERE user_role = 'role';) all in one statement?
I have tried the answer in the following question but that didn't work for me: INSERT INTO with SubQuery MySQL
Assuming all the user roles already exist, you can insert a new user with the proper user role id (i.e. SELECT user_role_id WHERE user_role = 'role';) in one statement with this query:
insert into users (user_role_id, email, password)
select
user_role_id,
'new_user',
'password_hash'
from user_role r
where r.user_role='role'
limit 1;
If subqueries work inside a values list you can try the first option below, but I suspect they don't. I'm also assuming colon is the parameter marker for your connection since.
insert into users (user_id, user_role_id, email, password)
values (
:user_id,
(select user_role_id from user_role where user_role = 'role'),
:email,
:password
)
Another option:
insert into users (user_id, user_role_id, email, password)
select
:user_id,
(select user_role_id from user_role where user_role = 'role'),
:email,
:password
--from dual or some other dummy table??
ASh's answer probably works great although I don't know why the limit 1 should be necessary. In that case you have two roles with the same name and you won't know which one comes back.

insert child mysql

I've got a newbie question...
I've got two tables:
parentTable
-----------
id_user int(11) not null auto increment primary key,
email varchar(64),
pass varchar(64)
childTable
----------
id_user int(11) not null,
name varchar(64),
address varchar(512),
foreign key (id_user) references parentTable(id_user)
on update cascade
on delete restrict
Now can I insert:
insert into childTable (id_user) select id_user from parentTable where id_user = '1'
But I just want to insert also name and address values.
Sorry for the newbie question, but I lurked for a day and found nothing.
Thank you in advance for your reply.
The interesting part about your query is that you know the id_user you're trying to select to insert - it's in your WHERE clause.
If you will always know the id_user, you can skip the extra SELECT portion of the query and directly do:
INSERT INTO childTable (id_user, name, address) VALUES (1, 'some name', '123 test street');
If you, for some other reason, need the additional SELECT, you can append the name/address values directly into the field-list, like this:
INSERT INTO childTable (id_user, name, address)
SELECT id_user, 'some name', '123 test street' FROM parentTable WHERE id_user = '1';