Does this code deletes duplicates? - mysql

I want to use this to get data from row out of mysql database into text files (one entry under another, 50 entries per file):
$ mysql --user=XXX --password=XXX --batch --skip-column-names \
-e "SELECT userid, displayname FROM Users" stackoverflowdb | \
split -l 50 -a 5 - "result."
but I also don't want to copy duplicate entries to these files. Will this code remove duplicates or do i need to add something to it to don't copy duplicate entries?

Modifying the SQL to use the DISTINCT directive
e.g.
SELECT DISTINCT userid,displayname FROM Users
will ensure that only unique combinations of userid and displayname are selected.
However this will not prevent userids that have identical displaynames.

SELECT DISTINCT userid,displayname FROM Users
.. or outside the database
mysql --user=XXX --password=XXX --batch --skip-column-names \
-e "SELECT userid, displayname FROM Users" stackoverflowdb | \
sort -u | \
split -l 50 -a 5 - "result."

Related

How to copy data from one table to other in same database?

In MySQL how to copy data from one table to another in the same database table?
I know insert into select, but it is taking forever to do this, especially on a live database, we can't take a risk.
Some conditions are there:
1. table1 is a source table and table1_archives is a destination table.
2. table1_archives already have data so we can only append.
My attempt:
time mysqldump --log-error=$logfile --complete-insert --insert-ignore
--no-create-info --skip-triggers --user=$dbuser --host=$host $dbname table1
--where="created < now()-interval 10 month" > $filename
But it has the name of table1, so I can't insert it into table1_archives.
Any guidance will be appreciated.
Thanks in advance.
In your output file, you need to change the table name table1 to table1_archives. Unfortunately mysqldump does not have any way to do this. You will have to do this on the fly using sed, which will rename everything in output file from table1 to table1_archives.
Since your columns can also contain the content like table1, its better to search and replace by enclosing them in backticks.
You can also use gzip to compress the output file.
Here is the command that worked for me
mysqldump -u USER -h HOST -p --skip-add-drop-table --no-create-info --skip-triggers --compact DB table1 |\
sed -e 's/`table1`/`table1_archives`/' | gzip > filename.sql.gz
"but it is taking forever to do this"
There is a small trick to avoid this and then insert into will work faster:
Insert into table1 select * from table2
Trick:
step-1: drop all indices from table2
step-2: execute query
step-3: create indices again

MySQL: Insert into a table from another HOST

So I would like to copy some records from one table to another. But the trick is that the another table is in a different HOST. I will try to explain by giving you a mysql query pseudo code.
Another_host = "192.168.X.X";
INSERT INTO database_original.table_1( id, name, surname)
SELECT id, name, surname
FROM Another_host.database_another.table_2
WHERE Another_host.database_another.table_2.id > 1000;
I would probably have to declare the user for the "Another_host" somewhere.
This is what I am trying to do..is this even possible like I imagine it?
Thx
There is one workaround solution which will do the same what you want.
Step 1:
Take dump of select query
mysql -e "select * from myTable" -h <<firsthost>> -u myuser -pxxxxxxxx mydatabase > mydumpfile.sql
Step 2: Restore the dump
mysql -h <<secondhost>> -u myuser -pxxxxxxxx < mydumpfile.sql

Using concat function in MySql in a shell script

I have the following script which fetches employee id from Employee table in every database allocated to the employee and alters the EmployeeLedger_$empId table in the corresponding database
cls_ip="localhost";
mysql="mysql -h $cls_ip -u root"
list=`echo "show databases like '$dbPattern'" | $mysql| grep -v Database`
for db in $list
do
echo "altering EmployeeLedger table for database $db";
${mysql} ${db} -e "use $db";
empId=`${mysql} ${db} -e "select EMPID from Employee"`;
echo "$empId";
${mysql} ${db} -e "alter table concat('EmpTimeLedger',$empId) add column HOLIDAY tinyint(1) not null default 1;";
done
Here I am unsuccessful in concatenating the employee id which i retrieve from Employee table with EmplyeeLedger to form EmployeeLedger_$empId table. How do i do it?
I think the easiest way to do this would be to simply concatenate the empId to the name of your table:
table="EmpTimeLedger_$empId";
Then you would alter your table like this:
ALTER TABLE $table
ADD COLUMN HOLIDAY TINYINT(1) NOT NULL DEFAULT 1;
why dont you try manual string concatenation like this
foo="EmpTimeLedger"
foo+=$empId
${mysql} ${db} -e "alter table $foo add column HOLIDAY tinyint(1) not null default 1;"

Store the results of a shell script in MySQL

I am having a simple shell script which will grep a sequence from the log and displays the result on terminal when i run the script. How can i store this result into a MySQL table directly in the script itself?
For Example:
#!/bin/bash
echo "Enter the two numbers to be added:"
read n1
read n2
answer=$(($n1+$n2))
echo $answer
In the above example, how can i store n1, n2 and answer values in a database table.
Create an insert query, and pipe it to stdin of mysql.
echo "insert into myTable values ($n1, $n2, $answer);" | mysql -u .. -h ..
First you need to have a table to hold the value of n1, n2, answer
suppose you have "mytable" in database "test" with 3 columns for n1 n2 and answer, then just insert like this:
echo "insert into test.mytable values ($n1, $n2, $n1+$n2);" | mysql -h hostname -u username -ppassword
or
mysql -h hostname -u username -ppassword -e "insert into test.mytable values ($n1, $n2, $n1+$n2);"

Getting MYSQL DB info via bash

I'm using some bash code which I got off another post on here
#!/bin/sh
users=$(mysql --user=user --password=password --database=$db -h _IP ADDRESS) -s -- execute="select$ users from db limit 1;"|cut -f1)
echo "$users"
The database is DB and the table is users basically I want to be able to get a user count from the table but when I run the script I get
ERROR 1049 (42000): Unknown database 'execute=select users from db limit 1;'
any idea what i'm doing wrong or a better way of doing it? if I do
select * form users;
on the mysql server itself it returns 12 rows in set (0.00 sec) 12 being the number of users, so I just want my script to query the user table on database DB and return the number of rows ie 12.
users_count=$(
mysql --user user_name --password=password -h x.x.x.x <<EOF | tail -n 1
select count(1) from mysql.user;
EOF
)
it seems that the order of your params is wrong...
MYSQL is telling you that 'execute=select users from db limit 1;' is in the position of the database parameter.
Try something like this:
mysql --user user_name --password=password -e 'select users from db_schema.table limit 1;'
Simply try this:
mysql -u username --password=password -h hostname database_name -e 'select count(1) from table limit 1;'