2010-11-03 167 views
1
加入他們

我期待加入兩列單...這裏有一個想法..如何從兩個不同勢列中選擇條件,從SQL

======================================================= 
    id | firstname |  lastname 
======================================================= 
    1 | Peter  | Das 
------------------------------------------------------- 
    2 | Das  | James 
------------------------------------------------------- 
    3 | vector  | Call 
______________________________________________________________ 
    4 | cans  | Das 

SQL查詢應該是這樣的(僞)

SELECT (lastname WHERE firstname='Das' AND firstname WHERE lastname='Das' 
    ORDER BY ID DESC) AS name 

名稱和ResultSet應該是這樣的

====================== 
     name 
    ====================== 
     cans 
    --------------------------------------- 
     James 
    --------------------------------------- 
     Peter 

現在誰能告訴我什麼是電子XACT查詢

每一個想法提示建議,歡迎

+0

你永遠也不會看到這兩個名字和姓氏是「達斯行「? – 2010-11-03 13:57:03

回答

4

看起來像一個工作,一個UNION查詢

SELECT id, firstName AS name 
FROM table 
WHERE lastName = 'Das' 
UNION ALL 
SELECT id, lastName AS name 
FROM table 
WHERE firstName = 'Das' 
ORDER BY id DESC 
+0

我想他可能是尋找目標只是一個普通的UNION而不是UNION,因爲他沒有在樣本集合返回任何重複。 – SqlRyan 2010-11-03 06:11:46

+1

我只是想提前到不可避免的「資本達斯」記錄 – Phil 2010-11-03 06:14:09

+0

由於它的工作...我已經檢查了它.. – 2010-11-03 06:58:41

13
SELECT CASE 
      WHEN firstname = 'Das' THEN lastname 
      ELSE firstname 
     END AS name 
    FROM table 
    WHERE firstname = 'Das' 
     OR lastname = 'Das' 
ORDER BY id DESC 
+0

感謝所有的你..它的工作..好聽 – 2010-11-03 06:47:16

+0

@Suman,隨時接受答案這對你最有效。 – Unreason 2010-11-03 08:11:07

+0

@蘇曼:一年半之後,你仍然沒有接受答案。爲什麼不? – 2011-07-04 09:23:52

2

你沒告訴我們你的表的名稱。假設它被稱爲mynames,這應該工作:

select case when firstname = 'Das' then lastname 
else firstname end as name from mynames 
where lastname = 'Das' or firstname = 'Das' order by id desc; 

這裏是MySQL的一些測試代碼,我寫驗證解決方案的工作原理:

mysql> create table mynames(
    -> id int unsigned primary key auto_increment, 
    -> firstname varchar(32) not null, 
    -> lastname varchar(32) not null 
    ->) engine=innodb; 
Query OK, 0 rows affected (0.45 sec) 

mysql> insert into mynames (firstname, lastname) values 
    -> ('Peter', 'Das'), 
    -> ('Das', 'James'), 
    -> ('vector', 'Call'), 
    -> ('cans', 'Das'); 
Query OK, 4 rows affected (0.49 sec) 
Records: 4 Duplicates: 0 Warnings: 0 

mysql> select * from mynames; 
+----+-----------+----------+ 
| id | firstname | lastname | 
+----+-----------+----------+ 
| 1 | Peter  | Das  | 
| 2 | Das  | James | 
| 3 | vector | Call  | 
| 4 | cans  | Das  | 
+----+-----------+----------+ 
4 rows in set (0.00 sec) 

mysql> select case when firstname = 'Das' then lastname 
    -> else firstname end as name from mynames 
    -> where lastname = 'Das' or firstname = 'Das' order by id desc; 
+-------+ 
| name | 
+-------+ 
| cans | 
| James | 
| Peter | 
+-------+ 
3 rows in set (0.00 sec) 
+0

只是好奇,是它強烈必要的認證Java/PHP開發人員創建一個表,填寫後進行查詢,以獲取**這樣的瑣事**查詢真的有用嗎? ;-) – zerkms 2010-11-03 06:29:45

+2

@zerkms:額外的嚴謹只需要2分鐘。從字面上看。把它看作是一個小小的SQL單元測試。 – Asaph 2010-11-03 06:33:12

+0

如你所願;-) BTW單元測試SQL通常爲落實** ** SP,而不是** **數據,只是因爲你的測試不是自動;-) – zerkms 2010-11-03 06:36:46

相關問題