Sunday, August 14, 2011

Mysql Tricks

  • How to get current time
select now();
select curdate();
select curtime();


  • How to get date in standard format

select date from table;

will give answer as "2011-08-15"
But

select date+0 from table;

will give answer as "20110815"

  • To use group by with a summary.


select col from .... group by col with rollup;

This will give a summary(addition) in the last row.

  • When performing string operations


Shell "*" => mysql "%"
Shell "?" => mysql "_"


  • Conditional statements in mysql


select (sum(case when shares>0 then shares else - end) from table;

  • Case-sensetive comparison
select 'A' like 'a';
1
select 'A' like binary 'a';
0


  • Using variables

set @total=0;select (@total := 2+@total) as total from table;

  • Checking table sizes


select table_schema "Database name", sum(data_length _ index_length)/1024/1024 "size", sum (data_free)/1024/1024 "Free space " from information_schema.TABLES group by table_schema;

  • Command line execution and output
bash# mysql -e "select * from table";
mysql> select * into outfile "/tmp/hpatil" from ...;

But watchout for mysqld host and users when searching for outfile on commandline.

Thursday, July 14, 2011

Linux/Unix NFS bug

Sometimes it seems that we are not able to 'touch' the file becaue of error "already exists", but we try to delete it we get an error "file does not exists".

To solve this issue,
We need to create a temp file.
mv to original file
and then delete this file.

Now, we will be able to create a new file in same location.

Friday, July 8, 2011

Finding database size through mysql query


SELECT table_schema "Data Base Name", sum( data_length + index_length ) / 1024 / 1024 "Data Base Size in MB" FROM information_schema.TABLES GROUP BY table_schema ;

My bashrc

This is a initial setup. You can work on things similar to these.





PATH=$PATH:.:~/hpatil/0/
export EDITOR=/usr/bin/vim

PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\t \[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
export HISTSIZE=10000
export HISTCONTROL=erasedups
shopt -s histappend

BC_ENV_ARGS=~/.bcrc
export BC_ENV_ARGS
export INPUTRC=~/.inputrc

#alias vim="vim -c 'set nopaste'"
alias ll="ls -l"
alias rl="readlink -f"
alias vim='vim -c"set nopaste" '
alias vr='vim -MR -c"set nopaste" '

alias li='less -niFX'


alias grep_dir='_(){ bd;grep -a "mt=j" app.$1.tlog | grep -av "not active" | li;}; _'

function mailme() {
    (echo "Attached file: $1"; uuencode "$1" "$(basename "$1")") | mail -s "File: $(basename "$1")" harshal.patil@email.com
}

function bc() {
    command bc -l <<< "scale=6; $*"
}

Tuesday, June 28, 2011

Setting autofs timeout option

This is the time after which autofs will unmount the volume if kept idle. It can be set in file /etc/sysconfig/autofs.

OPTIONS="--timeout=86400"

Friday, June 24, 2011

Compression bzip2 command

Good and simple compression command

find ./04 -type f | grep -v bz2 | xargs bzip2

Loading .bas file in excel

* Ctrl + Break to stop Macro / Alt + F8 to run "stop function" * Alt + F11 to open VBA editor * Remove old bas and import new bas ...