PHP, SQL & MySQL howto

Databaser / PHP, SQL & MySQL howto
Rename or change name of MySQL table

Rename a table:

RENAME TABLE table_name TO new_table_name;

SQL ALTER TABLE Statement
How to insert a new column between 2 column in a Table.This is for for MS SQL Server
Add a column to an existing MySQL table

Add new column at the end of the table:

ALTER TABLE table_name ADD column_name datatype;

For example: ALTER TABLE contacts ADD email VARCHAR(60);

Insert a new column between two columns in a table:

ALTER TABLE table_name ADD column_name datatype AFTER column_name;

For example: ALTER TABLE contacts ADD email VARCHAR(60) AFTER name;

If you want the new column to be first, use this statement:

ALTER TABLE contacts ADD email VARCHAR(60) FIRST;

Delete a column:

ALTER TABLE table_name DROP COLUMN column_name;

To change the data type of a column in a table, use the following syntax (for MySQL):

ALTER TABLE table_name MODIFY column_name datatype;

Add a column that uses the datatype DOUBLE:

ALTER TABLE table_name ADD column_name DOUBLE;

Add a column that uses the datatype DATETIME:

ALTER TABLE table_name ADD added_date DATETIME;

There is no way to insert current date or time in MySQL while creating tables.U have to insert the values urself.
ie. this is wrong: ALTER TABLE table_name ADD added_date DATETIME DEFAULT NOW()

Rename a column:

ALTER TABLE table_name CHANGE old_column_name new_column_name varchar (10);

SQL datatypes:
W3Schools - SQL Data Types
zetcode.com - MySQL data types

Copy a column:

SQL:
ALTER TABLE table_name ADD new_column VARCHAR(60);
- creates new target column
UPDATE TABLE table_name SET new_column = old_column;
- copies old_column to new_column

SQL ALTER TABLE Statement

To change the data type of a column in a table, use the following syntax:

SQL Server / MS Access:
ALTER TABLE table_name ALTER COLUMN column_name datatype;

My SQL / Oracle:
ALTER TABLE table_name MODIFY column_name datatype;

UPDATE tablename SET avatar = 'blank.jpg';
- sets the value of all cells in column avatar to blank.jpg
btw, here's the query syntax...

UPDATE tablename SET fieldname = 'newvalue' WHERE fieldname = 'oldvalue'

So, for the default Post Notications it should look like this...

Update wp_post_notification_emails SET gets_mail = '0' WHERE gets_mail = '1'

That will disable all email addresses until it's all back up and running. Then reverse the process to enable them again.

Update wp_post_notification_emails SET gets_mail = '1' WHERE gets_mail = '0'

UPDATE table_name SET field_name=REPLACE(field_name,’OLD’,'NEW’);

Example:
OLD=dog
NEW=1

My answer is repeating what others have said before, but I thought I'd add an example, using MySQL, only because the previous answers were a little bit cryptic to me.

The general form of the command you need to use to update a single row's column:

UPDATE my_table SET my_column='new value' WHERE something='some value';

And here's an example:

BEFORE

mysql> select aet,port from ae;
+------------+-------+
| aet        | port  |
+------------+-------+
| DCM4CHEE01 | 11112 | 
| CDRECORD   | 10104 | 
+------------+-------+
2 rows in set (0.00 sec)

MAKING THE CHANGE

mysql> update ae set port='10105' where aet='CDRECORD';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

AFTER

mysql> select aet,port from ae;
+------------+-------+
| aet        | port  |
+------------+-------+
| DCM4CHEE01 | 11112 | 
| CDRECORD   | 10105 | 
+------------+-------+
2 rows in set (0.00 sec)
how to search and replace within a column

For instance maybe you want to change all instances of the word “colour” (from string) with “color” (to string).

column beforecolumn after
a purple coloura purple color
colour greencolor green
bluish colourbluish color

UPDATE table_name SET column_name = REPLACE(column_name, "from string", "to string") WHERE column_name LIKE "%from string%";

MySQL: Retrieve Unique Values and Counts For Each

Use GROUP BY:

select value, count(*) from table group by value
Use HAVING to further reduce the results, e.g. only values that occur more than 3 times:

select value, count(*) from table group by value having count(*) > 3

Tizag.com - mysql aggregate functions - count()

table products:

idnametypeprice
123451Park's Great HitsMusic19.99
123452Silly PuddyToy3.99
123453PlaystationToy89.95
123454Men's T-ShirtClothing32.50
123455BlouseClothing34.97
123456Electronica 2002Music3.99
123457Country TunesMusic21.55
123458WatermelonFood8.73
<?php
// Make a MySQL Connection

$query = "SELECT type, COUNT(name) FROM products GROUP BY type"; 
	 
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
	echo "There are ". $row['COUNT(name)'] ." ". $row['type'] ." items.";
	echo "<br/>";
}
?>

There are 2 Clothing items.
There are 1 Food items.
There are 3 Music items.
There are 2 Toy items.

How to count values with MySQL queries

Let’s say you have the following table called votes that keeps track of how people voted and you want a query to count the number of votes for you instead of having to loop through all the rows with a counter in PHP.

person vote
obama yes
mccain no
obama yes
obama no
mccain yes
obama yes
obama yes
obama no
mccain no

Here is the MySQL query that would do just the job:

SELECT person,
SUM(IF(vote = "yes", 1,0)) AS `yes_votes`,
SUM(IF(vote = "no", 1,0)) AS `no_votes`,
COUNT(vote) AS `total`
FROM votes
GROUP BY person
ORDER BY yes_votes DESC

The following would be the result of the query:

person yes_votes no_votes total
obama 4 2 6
mccain 1 2 3

The beauty of the query all lies in the SUM IF statements. For example, in order to count the yes votes, the IF statement will check to see if vote = “yes” as it loops through all the rows. If so, the yes_votes column alias is incremented by 1. The same procedure goes for counting the no votes. The COUNT statements keeps a tally on the total number of votes. The GROUP BY person statement allows the vote counters to calculate the numbers by person instead of the total number of yes and no votes.

Now, you do not have to loop through your MySQL results to count column values =)