2016-12-02 25 views
0

有人能请我指出正确的方向吗?在MySQL/phpMyAdmin中选择与ID相反的单元格

鉴于表:

actor_uri_address 
--------------------- 
| ID | URI | Address| 
--------------------- 
| |  |  | 
--------------------- 

actor_full_name_opc 
----------------------------------------------- 
| ID | Given Name | Surname | Outward Postcode| 
----------------------------------------------- 
| |   |   |     | 
----------------------------------------------- 

我想生成包含演员URI,名字,姓氏和生活在名单上的地址相同的演员的地址表(我并不需要提供的地址在查询时间,而不是地址栏中的重复值)在phpMyAdmin上。

到目前为止,下面是我的查询结构。尽管做了所有的努力,但我只能使它运行并返回整个URI,名字,姓氏和地址。我完全停留在如何缩小仅限于住在同一地址的演员。

感谢您提前。

SELECT 
    `actor_uri_address`.`URI`, 
    `actor_full_name_opc`.`Given Name`, 
    `actor_full_name_opc`.`Surname`, 
    `actor_uri_address`.`Address` 
FROM 
    `actor_uri_address` 
LEFT JOIN `actor_full_name_opc` ON `actor_full_name_opc`.`ID` = `actor_uri_address`.`ID` 
GROUP BY 
    `actor_uri_address`.`URI`, 
    `actor_full_name_opc`.`Given Name`, 
    `actor_full_name_opc`.`Surname`, 
    `actor_uri_address`.`Address` 
+0

你希望所有的演员住在您指定的地址? –

+0

嗨三文鱼,我只是想要它输出一个演员的URIs,名字,姓氏和地址列表与一个或多个演员共享一个地址。有什么建议吗? –

+0

1 - 制定地址HAVING(你看到我在那里做了什么)1个以上的演员,2加入演员到这些地址。 –

回答

0

下面是一个例子使用(希望类似的数据)

CREATE TABLE `customer` (
    `id` int(11) DEFAULT NULL, 
    `version` decimal(10,2) DEFAULT NULL, 
    `title` varchar(20) DEFAULT NULL, 
    `FirstName` varchar(20) DEFAULT NULL, 
    `Middlenames` varchar(20) DEFAULT NULL, 
    `LastName` varchar(20) DEFAULT NULL, 
    `Gender` varchar(4) DEFAULT NULL, 
    `Dob` date DEFAULT NULL, 
    `Dod` date DEFAULT NULL, 
    `Warning_flag` varchar(2) DEFAULT NULL, 
    `Worth` varchar(2) DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

CREATE TABLE `customer_address` (
    `id` int(11) DEFAULT NULL, 
    `version` decimal(10,2) DEFAULT NULL, 
    `Customer_id` int(11) DEFAULT NULL, 
    `line1` varchar(50) DEFAULT NULL, 
    `line2` varchar(50) DEFAULT NULL, 
    `line3` varchar(50) DEFAULT NULL, 
    `line4` varchar(50) DEFAULT NULL, 
    `line5` varchar(50) DEFAULT NULL, 
    `postcode` varchar(10) DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

truncate table customer; 
insert into customer values 
(1,1,'Mr','fname1',null,'lname1','m','1990-01-01',null,null,null), 
(2,1,'Mrs','fname1',null,'lname1','f','1990-01-01',null,null,null), 
(3,1,'Mrs','fname1',null,'lname1','f','1990-01-01',null,null,null); 
truncate table customer_address; 
insert into customer_address values 
(1,1,1,'Line1','Line2','Line3','Line4','Line5','pc1'), 
(2,1,2,'Line1','Line2','Line3','Line4','Line5','pc1'), 
(3,1,3,'Line1','Line2','Line3','Line4','Line5','pc2'); 

所以,如果我想找到在同一邮政编码

MariaDB [bank]> select ca.postcode, count(*) obs 
    -> from customer_address ca 
    -> group by ca.postcode ; 
+----------+-----+ 
| postcode | obs | 
+----------+-----+ 
| pc1  | 2 | 
| pc2  | 1 | 
+----------+-----+ 
2 rows in set (0.00 sec) 

的客户数量,但如果我想限制它对那些有1个或更多客户

select ca.postcode, count(*) obs 
from customer_address ca 
group by ca.postcode having count(*) > 1 

现在我知道这是我可以让客户居住在这些邮政编码

MariaDB [bank]> select c.id,c.title, c.firstname, c.lastname,s.obs LivingAtThisPostcode 
    -> from customer c 
    -> join customer_address ca on ca.Customer_id = c.id 
    -> join 
    -> (
    -> select ca.postcode, count(*) obs 
    -> from customer_address ca 
    -> group by ca.postcode having count(*) > 1 
    ->) s on s.postcode = ca.postcode ; 
+------+-------+-----------+----------+----------------------+ 
| id | title | firstname | lastname | LivingAtThisPostcode | 
+------+-------+-----------+----------+----------------------+ 
| 1 | Mr | fname1 | lname1 |     2 | 
| 2 | Mrs | fname1 | lname1 |     2 | 
+------+-------+-----------+----------+----------------------+ 
2 rows in set (0.00 sec) 
+0

三文鱼,我会买你啤酒,如果现在能看到你。经过无数的努力,我终于碰到了问题!非常感谢你,你也可以在需要的时候找到帮助。 –

相关问题