PHP, SQL & MySQL howto

Databaser / PHP, SQL & MySQL howto
Copy all rows in a table column to a column in another table:
INSERT INTO table2 (name) SELECT name FROM table1
copy table from one database to another
USE db2;

CREATE TABLE table2 LIKE db1.table1;

INSERT INTO table2 SELECT * FROM db1.table1;
Copy table
USE database_name;

CREATE TABLE new_table LIKE old_table;

INSERT INTO new_table SELECT * FROM old_table;
Add a column to an existing MySQL table
ALTER TABLE contacts ADD email VARCHAR(60);

This first statement will add the email column to the end of the table. To insert the new column after a specific column, such as name, use this statement:

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;
MySQL:How to show data from 1 column which separated by comma?
SELECT column_name FROM table_name;
SELECT column_name FROM table_name LIMIT 10;
Copy 1 column to another by mysql query
I have mysql database with following table...

| id | amount | tax |
+----+--------+-----+
|  1 | 500    |     |
+----+--------+-----+
|  2 | 100    |     |
+----+--------+-----+

UPDATE table_name SET tax = amount

after you can remove it

ALTER TABLE mytable DROP COLUMN amount;

=== 

Or to rename instead of copying (much faster):

ALTER table_name CHANGE amount tax YOURDATATYPE;
MySQL: Updating a MySQL Record
update table_name set column_name = "newdata" where column_name = value;
Mysql Length Function
Query:

mysql> select length('Roseindia.net') as Lengthofstring;

Output:

+----------------+
| Lengthofstring |
+----------------+
|             13 |
+----------------+
1 row in set (0.00 sec)
MySQL select rows according to field length

I have some domain names stored in a database and I am trying to find domains which are only 3 characters in length.

I could use the MySQL function length(), but this would not return the required results as it would take into account the full length of the domain including extension (.com, .co.uk etc..).

Is there a way I can get 3 character domains from the database like so:

abc.com (would be returned)
def.com (would be returned)
ghi.com (would be returned)

jklm.com (would not be returned as it's 4 characters in length)

SELECT
  domain
FROM
  table
WHERE
  LENGTH(SUBSTRING_INDEX(domain, '.', 1)) = 3
PHP mysql_fetch_lengths() Function
<?php
$con = mysql_connect("localhost", "peter", "abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("test_db",$con);
$sql = "SELECT * from Person WHERE Lastname='Refsnes'";
$result = mysql_query($sql,$con);
print_r(mysql_fetch_row($result));
print_r(mysql_fetch_lengths($result));

mysql_close($con);
?>
how to delete multiple records with one query
$query = "DELETE FROM countries c WHERE c.id in(4,5,6,7,8,89,9, ....)";

$query = "DELETE FROM countries c WHERE c.id > 5  and c.id < 25";