mysql - 选择与 MySQL/phpMyAdmin 中的 ID 相同的值的单元格

标签 mysql sql

有人可以给我指出正确的方向吗?

给定表格:

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

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

我正在尝试生成一个表,其中包含居住在列表中相同地址的 Actor 的 URI、名字、姓氏和地址(我不需要在查询时提供地址,而是提供地址中的重复值)列)在 phpMyAdmin 上。

下面是到目前为止我的查询构造。尽管付出了一切努力,我还是只能让它运行并返回整个 URI、名字、姓氏和地址。我完全不知道如何将范围缩小到仅居住在同一地址的 Actor 。

提前感谢您。

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`

最佳答案

这是一个使用的示例(希望是类似的数据)

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)

关于mysql - 选择与 MySQL/phpMyAdmin 中的 ID 相同的值的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40931693/

相关文章:

mysql - 使用 MySQL Workbench 插入 POINT

mysql - Buzzfeed 类测验的数据库设计

mysql - 使用 mysqldump 注释指令

sql - 如何获取插入行的标识?

sql - 从选择命令创建一个新表

c# - SQL 时间戳到 C# 上的日期时间

MySQL删除语句报告Truncated incorrect datetime value

c# - SqlCommand 只返回一行

java - 如何在Hibernate中运行mysql更新批量查询?

mysql - 连接派生表时列不能为空错误