2014-01-25 109 views
0

我有以下的「TBLA」和「TBLB」表:得到所有其他用戶名,其中用戶= 1存在

CREATE TABLE if not exists tblA 
(
    id int(11) NOT NULL auto_increment, 
    sender varchar(255), 
    receiver varchar(255), 
    msg varchar(255), 
    date timestamp, 
    PRIMARY KEY (id) 
); 

CREATE TABLE if not exists tblB 
(
    id int(11) NOT NULL auto_increment, 
    sno varchar(255), 
    name varchar(255), 
    PRIMARY KEY (id) 
); 

INSERT INTO tblA (sender, receiver, msg,date) VALUES 
('1', '2', 'buzz ...','2011-08-21 14:11:09'), 
('1', '2', 'test ...','2011-08-21 14:12:19'), 
('1', '2', 'check ...','2011-08-21 14:13:29'), 
('1', '1', 'test2 ...','2011-08-21 14:14:09'), 
('2', '1', 'check2 ...','2011-08-21 14:15:09'), 
('2', '1', 'test3 ...','2011-08-21 14:16:09'), 
('1', '2', 'buzz ...','2011-08-21 14:17:09'), 
('1', '2', 'test ...','2011-08-21 14:18:19'), 
('1', '2', 'check ...','2011-08-21 14:19:29'), 
('1', '1', 'test2 ...','2011-08-21 14:10:09'), 
('3', '1', 'check2 ...','2011-08-21 14:21:09'), 
('3', '1', 'test3 ...','2011-08-21 14:22:09'), 
('3', '2', 'buzz ...','2011-08-21 14:24:09'), 
('3', '2', 'test ...','2011-08-21 14:25:19'), 
('1', '3', 'check ...','2011-08-21 14:26:29'), 
('1', '3', 'test2 ...','2011-08-21 14:27:09'), 
('2', '3', 'check2 ...','2011-08-21 14:28:09'), 
('2', '3', 'test3 ...','2011-08-21 14:29:09'), 
('1', '2', 'check3 ...','2011-08-21 14:23:09'), 
('1', '4', 'test2 ...','2011-08-21 14:27:09'), 
('1', '5', 'test2 ...','2011-08-21 14:27:09'), 
('2', '6', 'check2 ...','2011-08-21 14:28:09'), 
('2', '7', 'test3 ...','2011-08-21 14:29:09'), 
('8', '2', 'check3 ...','2011-08-21 14:23:09'); 


INSERT INTO tblB (sno, name) VALUES 
('1', 'Aa'), 
('2', 'Bb'), 
('3', 'Cc'), 
('4', 'Dd'), 
('5', 'Ee'), 
('6', 'Ff'), 
('7', 'Gg'), 
('8', 'Hh'); 

如何獲得與用戶的所有行:1與發件人名稱一起現有/接收機,它不是1(即:Aa

+0

什麼是用戶.. – Mihai

+0

用戶是我在這種情況下SNO = 1 name ='Aa' – jason

+0

所以,你想還沒有發送或接收任何郵件的用戶? – Shomz

回答

1

我認爲這將獲得獨特的名單:

select distinct b.name 
from tblA a join 
    tblB b 
    on b.id in (a.sender, a.receiver) and 
     ((b.id <> 1 and (a.sender = 1 or a.receiver = 1)) or 
     (a.sender = 1 and a.receiver = 1) 
     ); 
+0

您好先生,我準備將鏈接發佈給您。謝謝您的關注.http://www.sqlfiddle.com/#!2/49f8c/28。它顯示所有行,甚至沒有1個行。 – jason

+0

這個查詢不包括髮送者和接收者都是1的行。我也想包括這個。我該怎麼做?除此之外它的正確性。 – jason

+0

超級先生。謝謝。 – jason

0
SELECT ta.*, tb.name 
FROM tablA ta, tablB tb WHERE (ta.sender = tb.sno OR ta.receiver = tb.sno) 
         AND tb.sno <> 1 
0

像這樣做:

$result = mysqli_query($yourconnectiondata,"SELECT * FROM yourtable WHERE User=1"); 
while($row = mysqli_fetch_array($result)) 
{ 
    //Here you can get your receiver like this: 
    $myvar = $row['reciever']; 
} 
相關問題