Postgres - 如何检查空数组

时间:2009-04-10 13:49:30

标签: sql arrays postgresql

我正在使用Postgres,我正在尝试编写这样的查询:

select count(*) from table where datasets = ARRAY[]

即。我想知道有多少行对于某个列有一个空数组,但是postgres不喜欢这样:

select count(*) from super_eds where datasets = ARRAY[];
ERROR:  syntax error at or near "]"
LINE 1: select count(*) from super_eds where datasets = ARRAY[];
                                                             ^

5 个答案:

答案 0 :(得分:77)

语法应为:

SELECT
     COUNT(*)
FROM
     table
WHERE
     datasets = '{}'

使用引号和花括号来显示数组文字。

答案 1 :(得分:15)

您可以使用array_upper和array_lower函数在空数组上返回null的事实 ,所以你可以:

select count(*) from table where array_upper(datasets, 1) is null;

答案 2 :(得分:2)

Solution Query:
    var coord;

    function change_coord(location) {
    var geocoder = new google.maps.Geocoder();
      geocoder.geocode({'address': location}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
              //real_lat and real_lng are int values 
              var real_lat = results[0].geometry.location.lat();
              var real_lng = results[0].geometry.location.lng();
              coord = {lat: real_lat, lng: real_lng};
          }
      });
  console.log(coord); //undefined
  }
Example:

table_emp:

body {

	

	background-color: #558C89;

	/*background-color: #1FDA9A;*/

	color: #000305; /*remove if background is not working */

	font-size: 87.5%; /*base font size is 14px */

	font-family: Arial, 'Lucida Sans Unicode';

	line-height: 1.5;

	text-align: left;

	margin-left: 21.5%;

	

	

}

a {

	text-decoration: none;

}



a:link, a:visited {

	

}



a:hover, a:active {

	

}



.body {

	

	

	

}



form { 



display: inline; 



}

#logo {

	

	position: absolute;

	z-index: 1;

	left: 0;

	top: 5px;

	height: 50px;

	vertical-align: top;

	

	



	

}



.mainheader img {

		

		width: 100%;

		height: auto;

		margin: 0% 0%;



}



.mainheader nav {

		background-color: #424242;

		/*background-color: #008BBA; /* Here for color of navigated buttons*/

		height: 65px;

		width: 100%;

		position: absolute;

		top: 0;

		left: 0;

		border-radius: 5px;

		-moz-border-radius: 5px;

		-webkit-border-radius: 5px;

		border-style: solid;

		border-color: black;

		border-width: 1px;

	

}



.mainheader nav ul {

	

	list-style: none;

	margin: 0 auto;

	

}



.mainheader nav ul li {

	

	float: right;

	display: inline;

	margin-top: 0px;

	

}



.mainheader nav a:link, .mainheader nav a:visited {

	

	color: #FFF;

	font-weight: bold;

	display: inline-block;

	padding: 18px 25px;

	height: 10px;

	/* Come back here to continue to edit buttons */

}



.mainheader nav a:hover, .mainheader nav a:active, .mainheader nav .active a:link,

.mainheader nav .active a:visited {

	

	background-color: #638CA6;

	opacity: 0.85;

	height: 63px;

	text-shadow: none;

	

	

}



.mainheader nav ul li a {

	

	border-radius: 5px;

	-moz-border-radius: 5px;

	-webkit-border-radius: 5px;

	

}



.newsClass{

	position: absolute;

	width: 1000px;

	background-color: #404040;

	border-style: solid;

	border-color: black;

	border-width: 1px;

	text-align: center;

	color: #FFF;

	margin: -100px 0 0 -150px;

	top: 25%;

	left: 27%;

	z-index: 2;

	

	

}

.topcontent {

	background-color: #404040;

	width: 850px;

	position: absolute;

	text-align: center;

	color: #FFF;

	margin-top: 120px;

	border-style: solid;

	border-color: black;

	border-width: 1px;

	line-height: 1;

	display: inline-block;

	

	left: 21.5%;

	

}





#box{

    border: 1px solid rgb(200, 200, 200);

    box-shadow: rgba(0, 0, 0, 0.1) 0px 5px 5px 2px;

    background: rgba(200, 200, 200, 0.1);

    border-radius: 4px;

    top:50px;

}



h2{

    text-align:center;

    color:#fff;

}



#footer {

	

	color: #FFF;

	

}

答案 3 :(得分:2)

如果您像我一样在2020年发现这个问题,正确的答案是

select count(*) from table where cardinality(datasets) = 0

cardinality已添加到PostgreSQL 9.4中,即〜2015

https://www.postgresql.org/docs/9.4/functions-array.html

答案 4 :(得分:0)

SELECT  COUNT(*)
FROM    table
WHERE   datasets = ARRAY(SELECT 1 WHERE FALSE)