Archive for the ‘PHP / MySQL’ Category

How to update everyone’s profile page at facebook (same content for everyone)?

May 2, 2008

Adding index for a table

August 3, 2007

In Excel:

Approach #1: In the cell 1 of the index row, type in “1″ and cell 2, type in “2″. Select both cells. Excel highlights both cells with bold-stroked box. There is a small handler on the bottom-right corner (Supposed to be a feature called “Auto Fill”?) Hold and drag the handler all the way down to the last cell on the index row. Excel should autofill all the rest of the cells.

Approach #2: In the cell 1 of the index row, type in “1″. In cell 2, type in “=ROW_NUM1+1″ (For example, if this is row A, type in “=A1+1″. What this formula does is to increment the value of cell 1 by 1, which equals to 2.) Select cell 2; drag the small handler on the bottom-right to all the way down the last cell.

In MySQL:

Approach #3: Create a column which is primary key, data type of int and attribute of AUTO_INCREMENT.

convert string into date

June 23, 2007

I wanted to convert a column whose data type of string into that of date.

First add a column with data type of date;

Then:

UPDATE `myTable` SET `REALDATE`= str_to_date( `SRTDATE` , '%m/%d/%Y' );

This will convert ‘06/23/2007′ in STRDATE into ‘2007-06-23′ in REALDATE.

“COUNT”, group by” and “having”

April 1, 2007

To find out the total number of rows in the table:

SELECT COUNT(*) FROM blog_articles;

or (”blogID” being the primary key):

"SELECT COUNT(blogID) from blog_articles";

The differences between COUNT(*) and COUNT(col_name) are:

1. COUNT (*) and COUNT(col_name) will NOT necessarily return the same result. COUNT(col_name) will NOT return NULL rows, while COUNT(*) will;

2. Performance wise, there is a big difference as COUNT(*) is read from table info, while COUNT(col) really counts the rows. So COUNT(*) would be faster.

To find out if there are stories with different url sharing the same headline (”title”):

"SELECT * FROM blog_articles GROUP BY title HAVING COUNT(url)>1;"

To find out the total number of different headlines(”title”):

"select count(title) as count from (select title from blog_articles group by title) t;"

Basic mySQL commands

August 22, 2006

Establishing a DB connection, with PHP

1. mysql_connect()

2. mysql_select_db()

3. mysql_query()

4. mysql_fetch_array()

5. mysql_result()

6. mysql_error()

Basic SQL commands

CREATE TABLE

ALTER TABLE CHANGE/ADD/DROP

DROP TABLE

INSERT INTO ( , , , … ) VALUES ( , , , …);

UPDATE WHERE

SELECT FROM WHERE WHERE/GROUP BY/HAVING/ORDER BY LIMIT…

DELETE FROM WHERE

How to copy columns from one table into another table

May 8, 2006

Example:

INSERT INTO staff( firstName, lastName, address, homePhone, cellPhone, latitude, longitude )
SELECT firstName, lastName, address, homePhone, cellPhone, latitude, longitude
FROM gfxContact

Data normalization

May 6, 2006

I read about “database normalization” today in the book “PHP Essentials”, which introduces three of the five (?) “normal forms”. I don’t quite get it but I am glad that I came across it :)

I got some similar articles when smart googling “database normalization”. Here are some of them:

CSV to phpMyAdmin from Mac OSX

April 29, 2006

The CSV (comma delimited) file as shown in Word being exported from Excel (Mac version release 1):

The CVS file

The settings:

The settings in phpMyAdmin

The result showing the actual mysql commands:

The result showing the actual mysql commands