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
https://forums.phpfreaks.com/topic/219859-sub-query-with-as-keyword-help/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.