MySQL questions

Gerhard Häring gerhard at bigfoot.de
Tue May 7 19:59:40 EDT 2002


* Billy Ng <kwokng at earthlink.net> [2002-05-07 23:33 +0000]:
> The combination of the user_id and product_id will be uniqe.

You'd make this combination the primary key, then. Just add this to your
table definition: primary key(user_id, product_id) Fields used for a
PRIMARY KEY must be NOT NULL.

> When I get the
> product_id and user_id, i want to change the tableA to see if there is a
> record that has this user_id and product_id.  If not, I will insert a record
> with this user_id and product_id.  What I am doing right is:
> 
> Query to count tableA whether it has the user_id and product_id
> if count is 0
> insert tableA (user_id, product_id) values (xxxxx, xxxxxxx)
> 
> I think it must a way to write it in one SQL statement.

I don't think this is possible, INSERT and UPDATE are different beasts.
Sometimes, you can avoid evaluating the SELECT by just DELETEing the
entry, then INSERTing a new one. If there's no match to the WHERE clause
in the delete, it won't delete anything, so it's basically the
non-existant "INSERT OR UPDATE" that you seem to look for.

> > > Question3
> > > Can I query from a query in MySQL?
> > >
> > You mean can the output of one query become the input of another? Only if
> > you store the first query's output in a temporary table.

Yes, MySQL doesn't have subselects, which is one of it's many
weaknesses.

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))





More information about the Python-list mailing list