Click here to Skip to main content
15,920,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to insert some values to a new table based on another table fields value..
any one know about the correct syntax for that please help me..

i used this syntax .i know this is not valid.

INSERT INTO tbl_cust_payments
VALUES(null,'" + dates + "','" + sub_id + "','" + cust_no + "','" + amount + "')
WHERE NOT EXISTS (SELECT * FROM tbl_subscriber_account WHERE tbl_subscriber_account.amount <= 0)

tbl_cust_payments and tbl_subscriber_account are the two tables..
Posted

Hi, try this:

SQL
IF NOT EXISTS(SELECT * FROM tbl_subscriber_account WHERE tbl_subscriber_account.amount <= 0)
    INSERT INTO tbl_cust_payments
    VALUES(null, dates, sub_id, cust_no, amount) 



Hope it helps! :)
 
Share this answer
 
v3
Hi,
Try like this.
SQL
INSERT INTO tbl_temp2 (fld_id)
  SELECT tbl_temp1.fld_order_id
  FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;



for more details check this website
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html[^]

http://dev.mysql.com/doc/refman/5.6/en/insert-select.html[^]

http://stackoverflow.com/questions/9139003/insert-into-select-from-query-with-where-condition[^]
 
Share this answer
 
Comments
sabodh 19-Jul-13 3:41am    
thanks for the reply..
but i don't need the values from another table to insert i need to insert the values manually..
suggested codes are copy the values from another tables..
but i use the textfieds values from the UI..
syed shanu 19-Jul-13 3:51am    
yes you can insert any value It might be your own values or from the selected table values.
for example here i have a insert query with my own insert values in select statement with condition.
insert into Subul1
(Subul_Date,Prod_NO,Gicho,IP_Qty,OT_Qty,GIMAC)
SELECT '2013-07-12 00:00:00.000','S',10,10,40,40 from Subul WHERE GIMAC > 10;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900