I have a database with table named 'Customers' which it contains customer's phone number. Its definitely more than one row.
On the other hand, I also have database named 'SMS' with a table in it, called 'Outbox'.
In order to show all phone number from 'Customers' table, I use this SQL query :
SELECT Customers.PhoneNumber FROM Customers WHERE PurchaseDate BETWEEN 2012-01-01 AND 2012-01-31;
Now, I need those phone numbers (multiple rows) to be inserted into 'Outbox' at once. Every phone number will receive same message. so, basically I just need to know how to use this INSERT query for multiple rows :
INSERT INTO Outbox (PhoneNumber, Message) VALUES (*Multiple PhoneNumbers Here*, 'Same Message To All Customers');
any idea how to do it? thanks before.
I assume you want a separate row for each phone number. You can do this by combining INSERT with SELECT:
INSERT INTO db2.Outbox (PhoneNumber, Message)
SELECT Customers.PhoneNumber, 'Same Message To All Customers' as message
FROM db1.Customers
WHERE PurchaseDate BETWEEN 2012-01-01 AND 2012-01-31;
INSERT INTO Outbox (PhoneNumber, Message)
SELECT Customers.PhoneNumber, 'Same Message To All Customers' as Message
FROM Customers
WHERE PurchaseDate BETWEEN 2012-01-01 AND 2012-01-31;
Should do you
INSERT INTO dbX.table_a
SELECT ...
FROM dbY.table_b
WHERE ...
You don't have to specify anything in the inserts' value list if you make sure the selects' column list matches dbX.table_a entirely.
you can use (mysql_fetch_array) see these links below: