Page 1 of 1

INSERT and SELECT SQL statement

Posted: Tue Jun 23, 2009 3:36 pm
by anthor

Code: Select all

$query_rs_orderid = sprintf("INSERT INTO order_list (order_by,status,order_date)VALUES('%s',1,now())", $userid);
$rs_orderid = mysql_query($query_rs_orderid, $Conn) or die(mysql_error());
 
$query_rs_post = sprintf("INSERT INTO order_item (order_id,item_id)VALUES(%s) SELECT itemid FROM cart WHERE orderby=%s", mysql_insert_id(), $userid);
err.. second SQL statement i have no idea about this,
i want to inside all the itemid where orderby is = $userid,and the order_id will be the previous sql statement had inserted auto increase id that why i use mysql_insert_id(),the problem is at item_id,shall i use do while loop to insert?or any better solution?

Re: INSERT and SELECT SQL statement

Posted: Tue Jun 23, 2009 3:44 pm
by patrickmvi
I'm not 100% sure what you're trying to do from your description but I think for the second query, this is the format you might be looking for:

INSERT INTO order_item (order_id, item_id) SELECT '%s', itemid FROM cart WHERE orderby = %s

Hope that helps.

Re: INSERT and SELECT SQL statement

Posted: Tue Jun 23, 2009 4:04 pm
by anthor
oh god ! it is what i want ! i never seen this kind of SQL statement..oh thanks man !
i dont understand the statement meaning man,can explain please?

Code: Select all

SELECT '%s', itemid FROM cart WHERE orderby = %s
this is hard man

Re: INSERT and SELECT SQL statement

Posted: Tue Jun 23, 2009 10:15 pm
by patrickmvi
I'm having a hard time understanding what you're saying but basically when you include a string of text between single quotes as part of the field list of a select statement, it will just return it back to you as is which is how this statement works. So for instance if you just do:

Code: Select all

SELECT 'HELLO WORLD';
it would just return "HELLO WORLD". It's a little weird to understand but that's just how it works.