gacoksa Posted August 18, 2012 Share Posted August 18, 2012 hi all, help me please. CREATE TABLE `a_table` ( `id` int(11) NOT NULL, `phone` varchar(20) NOT NULL, `group` varchar(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; INSERT INTO `a_table` VALUES (1, '111', 'cd'); INSERT INTO `a_table` VALUES (2, '222', 'cd'); INSERT INTO `a_table` VALUES (3, '333', 'cd'); INSERT INTO `a_table` VALUES (4, '444', 'xy'); INSERT INTO `a_table` VALUES (5, '555', 'xy'); INSERT INTO `a_table` VALUES (6, '666', 'xy'); CREATE TABLE `b_table` ( `id` VARCHAR( 11 ) NOT NULL , `phone` VARCHAR( 20 ) NOT NULL , `message` VARCHAR( 50 ) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM ; I want to SELECT phone FROM a_table WHERE group =xy. and insert a string = "I LOVE YOU" to b_table(phone,message). condition row of b_table After Query/ Insert is: ----+----------------+----------------------+ id | phone | message | ----+----------------+----------------------+ 1 | 444 | I LOVE YOU | ----+----------------+----------------------+ 2 | 555 | I LOVE YOU | ----+----------------+----------------------+ 3 | 666 | I LOVE YOU | ----+----------------+----------------------+ how to create query for this problem. Thank you. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 18, 2012 Share Posted August 18, 2012 << Moving to MySQL forum >> INSERT INTO b_table (phone, message) SELECT (phone, 'I LOVE YOU') FROM a_table WHERE group = 'xy' 13.2.5.1. INSERT ... SELECT Syntax Quote Link to comment Share on other sites More sharing options...
gacoksa Posted August 18, 2012 Author Share Posted August 18, 2012 but, there is no field message in the a_table, 'I LOVE YOU' is a string. $message="I LOVE YOU"; Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 18, 2012 Share Posted August 18, 2012 but, there is no field message in the a_table, 'I LOVE YOU' is a string. $message="I LOVE YOU"; And who said we are selecting a "field" called `I LOVE YOU`? Did you notice that it is defined in that query as a String enclosed by single strait quote marks 'I LOVE YOU' and not a field with back ticks `I LOVE YOU`. Did you even take the 5 seconds needed to try the perfectly valid solution I provided you? No, because if you had you would have seen that it will work. It is perfectly valid to SELECT other things than fields. You can select static values, expressions, date/time, etc. etc. The MySQL manual is littered with such examples. mysql> SELECT 1 + 1; -> 2 Quote Link to comment Share on other sites More sharing options...
gacoksa Posted August 18, 2012 Author Share Posted August 18, 2012 $message="I LOVE YOU"; // INSERT INTO b_table (phone,message) VALUES ('Values of phone', 'Values of message') // Values of phone = values from phone field a_table. // Values of message = $message == "I LOVE YOU"; Quote Link to comment Share on other sites More sharing options...
gacoksa Posted August 18, 2012 Author Share Posted August 18, 2012 problem Query 18860_.zip Quote Link to comment Share on other sites More sharing options...
Barand Posted August 18, 2012 Share Posted August 18, 2012 You have also posted that in the PHP Help forum http://forums.phpfreaks.com/index.php?topic=364062.msg1723804#msg1723804 Don't double post - it just wastes our time. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 18, 2012 Share Posted August 18, 2012 gacoksa, did you even TRY what I provided you? It will do what you want - you just don't understand enough about SQL to know that it will and you are dismissing it out of hand. Not only is double posting a waste of time so is not trying the solutions provided. The code you are trying to use is worthless. You don't need a while loop. The format of the query I provided will work. This is ALL you need include ("koneksi.php"); $con = mysql_connect($servername,$username ,$password); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("smsd"); $group = mysql_real_escape_string(trim($_POST['group'])); $message = mysql_real_escape_string(trim($_POST['message'])); $balas_sms = "INSERT INTO outbox (phone_number, text) SELECT phone, '$message' FROM contacts WHERE group_name = '$group'"; $hasil_balas_sms = mysql_query($balas_sms); Quote Link to comment Share on other sites More sharing options...
jardrake Posted August 19, 2012 Share Posted August 19, 2012 Quick note here. You don't have to have 5 different insert queries. INSERT INTO `a_table` VALUES (6, '666', 'xy'),(6, '666', 'xy'),(6, '666', 'xy'),(6, '666', 'xy'),(6, '666', 'xy') Is the equivalent of 5 different inserts Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.