English 中文(简体)
事实没有显示我需要的所有结果。
原标题:Query doesn t show all the results that I need
  • 时间:2012-01-14 23:05:11
  •  标签:
  • mysql

我的表格如下:

+--------+--------+--------+--------+
|  Name  |Building|   Nr   |  Time  |
+--------+--------+--------+--------+
| Tim    | House  |   30   |  10:10 |
| Jill   | House  |   31   |  10:20 |
| Tim    | Flat   |   31   |  10:30 |
| NULL   | Tower  |  NULL  |  NULL  |
| Jack   | Hut    |   32   |  10:50 |
| Jane   | Cabin  |   35   |  10:60 |
| Susan  | Cabin  |   35   |  11:70 |
+--------+--------+--------+--------+ 

现在,我要展示一下Tim拥有的所有建筑物(及其数据)。 我也这样做:

SELECT * FROM `People` WHERE Name="Tim"

结果:

+--------+--------+--------+--------+
|  Name  |Building|   Nr   |  Time  |
+--------+--------+--------+--------+
| Tim    | House  |   30   |  10:10 |
| Tim    | Flat   |   31   |  10:30 |

迄今情况良好。 这就是说,有更多的建筑物(Tower、胡和Cabin)。 我也想显示这些内容,但留下数据(接受建筑类型)空白。

This result is that I need:

+--------+--------+--------+--------+
|  Name  |Building|   Nr   |  Time  |
+--------+--------+--------+--------+
| Tim    | House  |   30   |  10:10 |
| Tim    | Flat   |   31   |  10:30 |
| NULL   | Tower  |  NULL  |  NULL  |
| NULL   | Hut    |  NULL  |  NULL  |
| NULL   | Cabin  |  NULL  |  NULL  |
+--------+--------+--------+--------+

由于塔已经是NULI,我可以很容易地利用:

SELECT * FROM `People` WHERE Name="Tim" OR Name is NULL

结果:

+--------+--------+--------+--------+
|  Name  |Building|   Nr   |  Time  |
+--------+--------+--------+--------+
| Tim    | House  |   30   |  10:10 |
| Tim    | Flat   |   31   |  10:30 |
| NULL   | Tower  |  NULL  |  NULL  |
+--------+--------+--------+--------+

问题在于,我仍然怀念胡和Cabin。 即便是在问问问的情况下,还是能够做到这一点? 还尝试了一些办法,如按组别类分类,但结果却提供了混合数据。 此外,还利用谷歌搜索,但没有灯塔。

Hope anyone can help me out!

页: 1

 | NULL   | Hut    |  NULL  |  NULL  |
 | NULL   | Cabin  |  NULL  |  NULL  |

但是,当你增加更多的建筑时,这将是一项巨大的工作,可以列入这一表格。

最佳回答

Something like

select * from people where Name =  Tim  
union 
select  NULL , Building, NULL, NULL from   people where Name !=  Tim ;

你们怎么想要?

问题回答

你的表格没有显示时间(Tower、Huh、Cabin)在你所登的第一个询问中显示。 该数据库正在向各位提供正确信息。 如果时间是为了拥有其他类型的结构,数据需要加以反映。

你们可以通过思考你的询问来解决这个问题;你们正在问数据库两个问题:

(1) 显示Tim拥有的所有建筑类型

2) 显示我所有的楼房类型是Tim的。

这样做的最简单方式是http://dev.mysql.com/doc/refman/5.0/en/union.html” rel=“nofollow”>UNION 。 问题:

SELECT Name, Building, Nr, Time FROM People WHERE Name =  Tim 

UNION

SELECT DISTINCT NULL, Building, NULL, NULL FROM People WHERE Building NOT IN (SELECT Building FROM People WHERE Name =  Tim )

你们应该这样做。

确实奇怪的是,你是否确信没有哪一个民族或时代? 1. 使用越狱的询问:

SELECT * FROM `People` WHERE `Name`="Tim" OR `Name` is NULL OR `Name`= NULL  OR `NAME`=  

愿你们有力量,而不是真正的民族解放军。 仅举一.,就试图在田地名称中总是使用低地块。

SELECT IF(Name= Tim ,Name,NULL) AS Name,
       Building,
       IF(Name= Tim ,Nr,NULL) AS Nr,
       IF(Name= Tim ,Time,NULL) AS Time
FROM People ;

这应当给你带来期望的结果。





相关问题
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 ...

热门标签