English 中文(简体)
在发现元件时,我怎么用数字来印出“NUL”或“0”数值?
原标题:How do I print out NULL or 0 values for column values when an element isn t found?

我需要通过一套价值观(不到10项),看看它们是否被列入表。 如果是的话,我需要印刷所有记录价值,但如果该项目没有存在,我仍然希望将其列入印刷结果,尽管有联合国扫盲十年或零值。 因此,例如,以下询问:

select * 
  from ACTOR 
 where ID in (4, 5, 15);

+----+-----------------------------+-------------+----------+------+
| ID | NAME                        | DESCRIPTION | ORDER_ID | TYPE |
+----+-----------------------------+-------------+----------+------+
|  4 | [TEST-1]                    |             |        3 | NULL |
|  5 | [TEST-2]                    |             |        4 | NULL |
+----+-----------------------------+-------------+----------+------+
But I want it to return
+----+-----------------------------+-------------+----------+------+
| ID | NAME                        | DESCRIPTION | ORDER_ID | TYPE |
+----+-----------------------------+-------------+----------+------+
|  4 | [TEST-1]                    |             |        3 | NULL |
|  5 | [TEST-2]                    |             |        4 | NULL |
|  15| NULL                        |             |        0 | NULL |
+----+-----------------------------+-------------+----------+------+

这是可能的吗?

最佳回答

为了取得你想要的产出,你首先必须建造一个含有你所希望的<代码>ACTOR.id的衍生表格。 小型数据组的所有工程:

SELECT *
  FROM (SELECT 4 AS actor_id
          FROM DUAL
        UNION ALL
        SELECT 5
          FROM DUAL
        UNION ALL
        SELECT 15
          FROM DUAL) x

为此,您可向实际桌旁索取您想要的结果:

   SELECT x.actor_id,
          a.name,
          a.description,
          a.orderid,
          a.type
     FROM (SELECT 4 AS actor_id
             FROM DUAL
           UNION ALL
           SELECT 5
             FROM DUAL
           UNION ALL
           SELECT 15
             FROM DUAL) x
LEFT JOIN ACTOR a ON a.id = x.actor_id

如在<代码>x和a之间没有对应关系,则<代码>a栏将无效。 因此,如果在15岁前没有相应时间,你会命令为零:

   SELECT x.actor_id,
          a.name,
          a.description,
          COALESCE(a.orderid, 0) AS orderid,
          a.type
     FROM (SELECT 4 AS actor_id
             FROM DUAL
           UNION ALL
           SELECT 5
             FROM DUAL
           UNION ALL
           SELECT 15
             FROM DUAL) x
LEFT JOIN ACTOR a ON a.id = x.actor_id
问题回答

诚然,就这种微薄的价值观而言,你可以做这样的事情。

SELECT 
   *
FROM
   (
      SELECT 4 AS id UNION 
      SELECT 5 UNION 
      SELECT 15
    ) ids 
      LEFT JOIN ACTOR ON ids.id = ACTOR.ID

(That should work in MySQL, 我想;对于Oracle You d need to use DUAL, e.g. http://www.un.org/Depts/DGACM/index_french.htm

这只能使用临时表格

CREATE TABLE actor_temp (id INTEGER);
INSERT INTO actor_temp VALUES(4);
INSERT INTO actor_temp VALUES(5);
INSERT INTO actor_temp VALUES(15);
select actor_temp.id, ACTOR.* from ACTOR RIGHT JOIN actor_temp on ACTOR.id = actor_temp.id;
DROP TABLE actor_temp;

如果你知道对身份证的上限和下限,那不会太坏。 找到所有可能女方的意见——通过trick子连接是最简单的方式——并且是外人与你的真正表态。 在此,我把它限于价值1-1000。

select * from (
   select ids.id, a.name, a.description, nvl(a.order_id,0), a.type
   from Actor a,
        (SELECT level as id from dual CONNECT BY LEVEL <= 1000) ids
   where ids.id = a.id (+)
)
where id in (4,5,15);

您能否提出一个包含预期行为者的表格?

如果是的话,你可以离开。

SELECT * FROM expected_actors LEFT JOIN actors USING (ID)




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签