Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

How to use OR and AND conditions in a single query?

Today when I was coding for one of my projects, I required to use OR and AND queries together. And with having good experience in Mysql and PHP Programming, I thought I can do it easily. But when I did use the query, It was not working as expected. After analyzing my query, I got to know that it was checking AND and OR conditions separately and giving result even when AND condition is false because I forgot to use Bracket with OR conditions.

My Requirement: Select employee_id of all employers with common name 'John' , age>24 or city London

Name should be John
Age or City condition (One of them should be true)

Problematic Query: 
$sql = "Select employee_id from employers where employers.employee_name = '$name' AND employee_age > '$age' OR employee_city = '$city'";

It gives me result set with names other than John

Solution Query:
$sql = "Select employee_id from employers where employers.employee_name = '$name' AND (employee_age > '$age' OR employee_city = '$city')";

Updating all values using Single Query

Table which needs modification: TableA
Column whose value we need to change: ColA
Present Value: 2
New Value : 3

Query will be

UPDATE TableA 
SET ColA = '3' 
WHERE ColA = '2' 

How to import database using Mysql query browser in ubuntu?

Many times we want to shift our database from one machine to another. I faced similar situation when I want to shift my database from external server to my own. While shifting , I was using ubuntu operating system and mysql query browser as a tool. Though, it is pretty easy but I think it would be useful information for you.

Step 1 . Open Mysql Query Browser

Step2. Right click in Schemeta Option and select create Schema

Step3. Put your database name

Step4. Now Click on File(Alt + F)  and select open script

Step5. Select script which you want to import and click on execute.


Its done!

I Hope that helped

How to setup mysql database.yml in Ubuntu ?

Steps which I am sharing with you has worked for me and I hope that would help you in connecting your mysql database to your ruby on rails application.

Step 1. Open terminal and execute this command : sudo apt-get install mysql-server libmysqlclient-dev libmysql-ruby

Step2. Change Directory to your application folder ,like cd /rails_projects/cg

Step3. Now ,install mysql gem : sudo gem install mysql

Step4. Configure your database.yml :  sudo gedit configure/database.yml

Step5.Paste this code:
development:
  adapter: mysql
  encoding: utf8
  database: yourdatabase_development
  pool: 5
  username: root
  password: yourpassword


test:
  adapter: mysql
  encoding: utf8
  database: yourdatabase_test
  pool: 5
  username: root
  password: yourpassword



production:
  adapter: mysql
  encoding: utf8
  database: yourdatabase_production
  pool: 5
  username: root
  password: yourpasswod


Done! Now create database using rake:db create  and enjoy!