Step1
Gather some basic information about the data you want. Know what information the database hold as well as which table you want to query, what rows the table holds and which of those rows you want to see. Step2
Start with a very simple query which can then be built upon. This query will return every column and every row of the "customers" table, showing the basic form of the SELECT command. The amount of data returned will be overwhelming if your customer table is large. Example:
SELECT * FROM customers; Step3
Define what it is you want to do with the information your query will return. Remember, you won't usually need all columns of data. Step4
Return only wanted columns. To return only a few columns, replace the asterisk in the previous command with a comma-separated list of columns you want. For example, if you're doing a mailing to all of your customers, you will only need the name and address columns. Example:
SELECT name,address FROM customers; Step5
Narrow your query further by returning only rows that interest you, using a WHERE clause. The WHERE clause will cause the SELECT query to return only rows that satisfy the WHERE clause. This query will select the name, address and balance of every row in the database with a negative balance. Example:
SELECT name,address,balance FROM customers WHERE balance <> - In MySQL, as in other programming languages, the asterisk (
- ) is a wild card used as a shortcut to return all the columns from a table.
No comments:
Post a Comment