Dexter
Dexter

Reputation: 13

How do I use 2 WHERE clauses including two tables wherein a column in a Query

I try to use different columns within different tables. Like I want it to run the query If or Where [table.column]

users.username = 'ExampleUsername' AND users.cardnumber = ''

I don't think I can use NULL instead of '', because its an empty text string?

users.cardnumber = NULL

Anyways, I couldn't come further as this:

INSERT INTO users (cardnumber, hasone)
WHERE users.username = 'ExampleName' AND users.cardnumber = ''
SELECT number, sethasone
FROM cards
WHERE cards.used = '0'
LIMIT 1

I'm a bit of new with SQL, but after I got it right I could put the code into my php script.

- SOLVED! :

I've used two queries for each column.

update users
set hasone=(select sethasone from cards where used='0' LIMIT 1)
where username='TestUser'

and

update users
set cardnumber=(select number from cards where used='0' LIMIT 1)
where username='TestUser'

then I just deleted the row from cards and I was done.

delete from cards
where used = '1'
LIMIT 1

I gave the user a cardnumber from the table cards and delete that row in cards.

Upvotes: 0

Views: 116

Answers (2)

Pete_Gore
Pete_Gore

Reputation: 634

Well, I think that you're trying to re-create a JOIN between 2 table. What you need to do is to add a "card_id" field into the users table. Then to get the user AND the card you can do something like :

SELECT * FROM users u LEFT JOIN cards c ON c.id = u.card_id

Upvotes: 0

Manav
Manav

Reputation: 551

I think you are trying to write a nested query but you didn't know how to write it. If you want to write select query within insert or update query so before doing this Click here to read about sub-query or nested query.

Upvotes: 1

Related Questions