PHP, SQL & MySQL howto
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
How do i copy one fields value to another field using mysql?
How to change a column value in mysql
Have to change a field value for a number of entries-- MySQL
How to replace data in MySQL field
How to Change a Column Name in MySQL
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
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;
- sets the value of all cells in column avatar to blank.jpg
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'
Example:
OLD=dog
NEW=1
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)
For instance maybe you want to change all instances of the word “colour” (from string) with “color” (to string).
column before | column after |
---|---|
a purple colour | a purple color |
colour green | color green |
bluish colour | bluish color |
UPDATE table_name SET column_name = REPLACE(column_name, "from string", "to string") WHERE column_name LIKE "%from string%";
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:
id | name | type | price |
---|---|---|---|
123451 | Park's Great Hits | Music | 19.99 |
123452 | Silly Puddy | Toy | 3.99 |
123453 | Playstation | Toy | 89.95 |
123454 | Men's T-Shirt | Clothing | 32.50 |
123455 | Blouse | Clothing | 34.97 |
123456 | Electronica 2002 | Music | 3.99 |
123457 | Country Tunes | Music | 21.55 |
123458 | Watermelon | Food | 8.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 =)