0

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.

Edited by gacoekchip.pokher

3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by veedeoo
0

@gacoekchip.pokher

If you just want to add the text string in column "message" of table "b_table", then you can prepare your query like this... not the most efficient, but I want to leave some space for you to experiment in these very important aspects of PHP and MySql.

To Select phone and group from "a_table", we can prepare our query as simple as this.

$query = ("SELECT * FROM `a_table` WHERE `phone` = '". $phone ."' AND 'group' = '". $group ."'") ;

After getting some result ( I strongly suggest to use mysql_num_rows), then perform an "update" query. In your case , the update query can be like this,,

$query = ("UPDATE b_table SET amessage ='". $message ."' WHERE phone ='". $phone .'") ;

IMPORTANT! If you just need to update, then single statement may suffice. Use query below instead.

 $query = ("UPDATE b_table SET amessage ='". $message ."' WHERE `phone` = '". $phone ."' AND 'group' = '". $group ."'") ;

There you have it :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.