如何在MS Access中向SQL查询添加自定义行?

时间:2015-06-23 14:08:42

标签: sql ms-access

我尝试使用UNION,但我曾经收到过这样的消息:

  

查询输入必须至少包含一个表或查询

我试过的查询是:

$products = array(... 40000 products ...);
?>
<table class="table table-bordered">
    <thead>
        <tr>
            <th></th>
            <th>#</th>
            <th>Product name</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($products as $product): ?>
        <tr>
            <td><input type="checkbox" /></td>
            <td><?= $product->id; ?></td>
            <td><?= $product->name; ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

两个select语句都可以独立运行。

4 个答案:

答案 0 :(得分:1)

使用Dual表格

SELECT NULL AS ID, NULL AS Item
FROM dual
UNION ALL
SELECT i.ID AS ID, i.Item AS Item
FROM Items i
INNER JOIN People p ON i.PID = p.PID
WHERE p.Name = 'John';

答案 1 :(得分:1)

在MS Access中,您需要使用kludge。这可能有效:

SELECT TOP 1 NULL AS ID, NULL AS Item
FROM (SELECT TOP 1 *
      FROM ITEMS
      ORDER BY ID
     ) as i
UNION ALL
SELECT Items.ID AS ID, Items.Item as Item
FROM Items INNER JOIN
     People
     ON Items.PID = People.PID
WHERE People.Name = 'John';

子查询的唯一目的是获取一行表。请记住,如果存在联系,MS Access中的TOP 1可以返回多行。

答案 2 :(得分:0)

您需要指定您想要数据的表格。试试这个:

SELECT NULL AS ID, NULL AS Item
FROM Items INNER JOIN People ON Items.PID=People.PID
UNION 
SELECT Items.ID AS ID, Items.Item as Item
FROM Items INNER JOIN People ON Items.PID=People.PID
WHERE People.Name='John';

答案 3 :(得分:0)

如果将联合查询顺序更改为以形成的查询开头并且将遵循以下空查询,则可能

SELECT Items.ID AS ID, Items.Item as Item
FROM Items 
INNER JOIN People 
ON Items.PID=People.PID
WHERE People.Name='John'

union 

SELECT NULL AS ID, NULL AS Item