Tuesday, March 12, 2013

How to SELECT data from database - MySQL example

How to select data from mysql?

Short example in MySQL.

First: create new table in MySQL.


CREATE TABLE `cars` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `make` varchar(50) DEFAULT NULL,
  `model` varchar(50) DEFAULT NULL,
  `year` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

After that insert few rows of data to the table.
Below is example how to do it.
INSERT INTO
        cars
        (
            make,
            model,
            year
        )
    VALUES
        (
            "Opel",
            "Vectra",
            "2012-05-25"
        );
Now we can retrive data from MySQL:

SELECT
    id,
    make
FROM
    cars;
or (not good - because slower)
SELECT
    *
FROM
    cars;

No comments: