有人可以请我指出正确的方向吗?
给出表格:
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)
以下是使用(希望类似的数据)
的示例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)