将两个结果合并到一个表中

时间:2018-04-10 11:44:53

标签: sql postgresql

如果我有

SELECT count(id) FROM students;

count
---------
123

SELECT count(id) FROM staff;

count
---------
456

我希望它像跟随

nstu  | nsta
--------------
123   | 456

我该怎么做?

5 个答案:

答案 0 :(得分:1)

<div class="list-group">
    <h3>Name</h3>
<?php 
$column = array();
$query = "select name from info_user where user_status = '1'";  
$rs = mysqli_query($con,$query) or die("Error : ".mysqli_error());
while ($color_data = mysqli_fetch_assoc($rs)) {
    $column = array_merge($column, explode(',', $color_data['name']));
}
$column = array_unique($column);
foreach ($column as $value) {
?>
      <a href="javascript:void(0);" class="list-group-item"> 
      <input type="checkbox" class="item_filter colour" value="<?php echo $value; ?>" >
      &nbsp;&nbsp;<?php echo $value; ?>
      </a>
<?php } ?>  
</div> 

对于Oracle

SELECT (SELECT count(id) FROM students) as nstu ,(SELECT count(id) FROM staff) nsta;

答案 1 :(得分:1)

library(stringr)
sapply(str_split(colnames(df), "_",  n = 2), `[`, 2)
#[1] "P32_1" "PN32" 

答案 2 :(得分:1)

执行cross join

select s1.nstu, s2.nsta
from
(SELECT count(*) as nstu FROM students) s1
cross join
(SELECT count(*) as nsta FROM staff) s2

答案 3 :(得分:1)

只需使用子查询

SELECT * FROM  
    (SELECT count(id) FROM students) as nstu ,
    (SELECT count(id) FROM staff) nsta; 

答案 4 :(得分:0)

使用union而不是子查询(MSSQL)的另一种方法

SELECT SUM(Students) AS 'Students' ,SUM(Staff) AS 'Staff'
FROM
(
    SELECT 'Student' AS 'Type', count(id) AS 'Students',0 AS 'Staff' FROM students
    UNION ALL
    SELECT 'Staff' AS 'Type', 0 AS 'Students',count(id) AS 'Staff' FROM staff
) AS Subset