2

I tried using UNION but I used to get this message:

Query input must contain at least one table or query

The query I tried was:

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

Both select statements run OK independently.

4 Answers4

1

Use a Dual table

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';
Matt
  • 14,906
  • 27
  • 99
  • 149
1

In MS Access, you will need to use a kludge. This might work:

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';

The only purpose of the subquery is to get a table with one row. Remember that TOP 1 in MS Access can return more than one row if there are ties.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

You need to specify what table you want your data from. Try this:

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';
0

Perhaps if the union query order is changed to start with the formed query and the null query to follow:

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
Avagut
  • 924
  • 3
  • 18
  • 34