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 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

plus 1 for teaching me a new word "kludge"... and a good answer ofc
0

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

6 Comments

I don't believe Access has the Dual table, though if you join your answer with HansUp's from here: stackoverflow.com/questions/7933518/… then it should work.
It does use DUAL in Access... @Newd
@Hituptony Are you sure? Do you happen to have a link that references that?
@Newd you have to create it, but it can be done. gpgonaccess.blogspot.com/2010/03/making-dual-table.html
@Hituptony I would go ahead and click the link that I posted and review the answer I mentioned :-)
|
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';

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.