Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

Tuesday, February 24, 2015

MySQL INSERT INTO VALUES and SELECT multiples row of data

Found  a way to insert into a table some value i've and one i get from a select from other table


INSERT INTO table1 VALUES ("string", 10, [int]).

I  have the value of ''string" and the 10, but i've to find that [int] from a select like this:

SELECT idTable2
FROM table2
WHERE ...
Solutions:
Use an insert ... select query, and put the known values in the select:

insert into table1
select 'A string', 5, idTable2
from table2
where ...
Issue that may come out:

A problem I've found with this approach is that if the SELECT returns zero records then the statement succeeds but no data is INSERT. –  Neil C. Obremski Feb 13 '14 at 1:52

Wednesday, May 29, 2013

Initate MySQL

1. Start mysql.exe, the MySQL monitor:
C:\Program Files\mysql\mysql-5.0.77-win32\bin> mysql.exe -h localhost --user=root -p
2. Create the user and set the password:
mysql> CREATE USER theusername@localhost IDENTIFIED BY 'thepassword';
3. Make sure the new user appear in the user table:
mysql> SELECT * FROM mysql.user;
4. Create the database:
mysql> CREATE DATABASE yourdatabase;
5. Make sure your database was created:
SHOW DATABASES;
6. Grant all privileges for your new database to your user:
mysql> GRANT ALL ON yourdatabase.* to theusername@localhost;
7. Done. Now you got a new database and a new user with all privileges.