开发者

Simple SQL Server query clarification on finding a record using two conditions

I'm pretty new to SQL and would like some help with the following question: What query would I run to find records that have a first name of "John" AND a last name of "Doe"?

Simple SQL Server query clarification on finding a record using two conditions

If I try this, it doesn't work:

select * from tableNames
where (FName = 'John') and (LName = 'Doe')

Thanks,开发者_高级运维 Ray


Following (exact same) query as yours give the results you specify.

    ;WITH q AS (
      SELECT ID = 1, Fname = 'John', Lname = 'Doe'
      UNION ALL SELECT 2, 'Barry', 'Singer'
      UNION ALL SELECT 3, 'John', 'Doe'
      UNION ALL SELECT 4, 'James', 'Brown'
    )
    SELECT  *
    FROM    q
    WHERE   Fname = 'John' AND Lname = 'Doe'

Results

 ID          Fname Lname  
 ----------- ----- ------ 
           1 John  Doe    
           3 John  Doe    
(2 rows affected)


Seems that your query should work. Are you sure you have typed the table name correctly? What error are you receiving? This should also work, just replace tableName with the name of your table:

SELECT * FROM tableName WHERE Fname = 'John' AND LName = 'Doe'

Here's an interactive example showing that your original query should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜