Jump to content

Sub Query with AS Keyword Help


Link

Recommended Posts

Hi all!

 

I always confuse myself with subqueries and the AS keyword.  When I run the follow query, I get a uid does not exist (even when I wrap it with `uid`).  ERROR: #1054 - Unknown column 'uid' in 'where clause'

 

SELECT `rmnd_main`.`users`.*, 
`rmnd_main`.`users`.`id` AS uid 
FROM `rmnd_main`.`users` 
WHERE `rmnd_main`.`users`.`password` = 'PASS' 
AND 
  ( 
    (SELECT COUNT(*) FROM `rmnd_main`.`phones` WHERE `user_id` = uid AND `number` = 1234567890) = 1 
    OR 
    (SELECT COUNT(*) FROM `rmnd_main`.`emails` WHERE `user_id` = uid AND `email` = 1234567890) = 1 
  )

Basically have three tables and wait to see if this value (which is an email address or a phone) is in either the phones or emails table and that the password matches in the users table.  I can always just check the variable to see if it is an integer or contains an @ and make assumptions based on that, but I love learning how to properly write these queries.

 

Let me know, and thanks in advance!

 

Link to comment
Share on other sites

SELECT `rmnd_main`.`users`.*,
`rmnd_main`.`users`.`id` AS uid
FROM `rmnd_main`.`users`
WHERE `rmnd_main`.`users`.`password` = 'PASS'
AND
  ( EXISTS 
    (SELECT * FROM `rmnd_main`.`phones` WHERE `user_id` = uid AND `number` = 1234567890)
      OR
   EXISTS (SELECT * FROM `rmnd_main`.`emails` WHERE `user_id` = uid AND `email` = 1234567890) 
)
  

Of course, you shouldn't be sending passwords in plain-text across the network.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.