English 中文(简体)
在多张表格上设定对初专干事系统的缺省值,并可能出现空出结果?
原标题:Setting a default value for JOINS on multiple tables with possible empty resultsets?
  • 时间:2010-12-02 02:17:20
  •  标签:
  • mysql

因此,我试图以同样的id子加入多个,每个桌子可能或不可能有这种id。

SELECT a.value, b.value, c.value, d.value FROM tbl_a a 
JOIN tbl_b b ON a.id=b.id 
JOIN tbl_c c ON a.id=c.id
JOIN tbl_d d on a.id=d.id
WHERE a.id=123

显然,这种情况之所以失败,是因为如果 t子 t不入,它就留下了一个空洞的结果,而加入者失败。

我曾尝试过各种左派,外派加入,无法工作。 我也试图确定这两个条款如下:WHERE a.id=123 OR b.id=123 OR :, 但那是没有工作。

我尝试了一种模糊不清的主意,但这种定论使产出分立。

SELECT count(*), "a", IFNULL(a.value,0) FROM tbl_a a WHERE a.id=123
UNION
SELECT count(*), "b", IFNULL(b.value,0) FROM tbl_b b WHERE b.id=123
UNION
etc...

任何想法?

最佳回答

添加其他三个<代码>id 页: 1 WHERE 加入:OR,然后加入。

问题回答

Another approach is to create a derived table containing your desired key and LEFT JOIN-ing to all the other tables:

SELECT a.value, b.value, c.value, d.value 
FROM ( select 123 as id ) as dummy
LEFT JOIN tbl_a a ON dummy.id=a.id
LEFT JOIN tbl_b b ON dummy.id=b.id 
LEFT JOIN tbl_c c ON dummy.id=c.id
LEFT JOIN tbl_d d ON dummy.id=d.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 ...

热门标签