[英]how to select column with null value and use the id of the column to select another column of another table
consider table req and orders 考虑表需求和订单
CREATE TABLE `req` (
`req_id` bigint(20) NOT NULL,
`order_date` timestamp NULL DEFAULT NULL,
`status` varchar(20) COLLATE utf8_bin DEFAULT NULL,
`order_id` bigint(20) NOT NULL,`
PRIMARY KEY (`req_id`),
KEY `order_id` (`order_id`),
CONSTRAINT `req_ibfk_16` FOREIGN KEY (`order_id`) REFERENCES `order` (`order_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*!40101 SET character_set_client = @saved_cs_client */;
CREATE TABLE `orders` (
`order_id` bigint(20) NOT NULL,
`acc_id` bigint(20) DEFAULT NULL,
`acc_name` varchar(256) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`order_id`),
KEY `index_name` (`order_id`,`account_id`),
CONSTRAINT `order_ibfk_1`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*!40101 SET character_set_client = @saved_cs_client */;
"To select column oder_id, oder_date and status where status is null value of table req and use the oder id to select columns acc_id and acc_name of orders table" ? “要选择列oder_id,oder_date和status其中status为null req的值,并使用oder id选择acc_id列和acc_name of orders表”?
and diplay them jointly as 并将它们联合起来作为
status , oder_id , acc_id, acc_name
SELECT status,order_date,order_id FROM req WHERE status is null;
but i have problem on how to select another table column using the id and show them jointly 但我有如何使用id选择另一个表列并联合显示它的问题
i have used this for select column from one table 我用它来从一个表中选择列
SELECT status,order_date,order_id FROM req WHERE status is null;
i expect a output of select columns mentioned below 我期望下面提到的选择列的输出
status , oder_id , acc_id, acc_name status,oder_id,acc_id,acc_name
You need to join the two table by using the relationship that they have Based on your table, order_id is primary key of 'orders'
and is the FK of 'req'
您需要使用它们具有的关系来加入这两个表基于您的表,order_id是
'orders'
主键,并且是'req'
的FK
Use this code: 使用此代码:
SELECT
req.status , req.oder_id , orders.acc_id, orders.acc_name
FROM
req, orders
WHERE
req.order_id = orders.order_id
and
req.status is null;
This basically joins the two tables and you need to write what table the fields where come from. 这基本上连接了两个表,你需要写出字段来自哪个表。 This is the long version of the code but its the easiest way to understand.
这是代码的长版本,但它是最容易理解的方法。
let me know if you have any questions. 如果您有任何疑问,请与我联系。
Edit: Adding another approach by not Comma separated join. 编辑:通过非逗号分隔连接添加另一种方法。 Use full join for instead.
请改用完全联接。
SELECT
req.status , req.oder_id , orders.acc_id, orders.acc_name
FROM
req
Full Join
orders
on
req.order_id = orders.order_id
WHERE
req.status is null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.