PHP, SQL & MySQL howto
INSERT INTO table2 (name) SELECT name FROM table1
USE db2; CREATE TABLE table2 LIKE db1.table1; INSERT INTO table2 SELECT * FROM db1.table1;
USE database_name; CREATE TABLE new_table LIKE old_table; INSERT INTO new_table SELECT * FROM old_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;
SELECT column_name FROM table_name;
SELECT column_name FROM table_name LIMIT 10;
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;
update table_name set column_name = "newdata" where column_name = value;
Query: mysql> select length('Roseindia.net') as Lengthofstring; Output: +----------------+ | Lengthofstring | +----------------+ | 13 | +----------------+ 1 row in set (0.00 sec)
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 $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); ?>
$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";