1

I am building a query to gather data from multiple tables. I want to grab all of the item_ids from one table and then build the results by gathering other data from other tables based on that item_id. I could have sworn that this was possible; but, I can't seem to get it to work. Here is an example of what I'm trying to do:

SELECT item_id AS mainID, 
 (SELECT anotherField 
  FROM anotherTABLE 
  WHERE item_id=mainID) 
FROM theMainTable;

Granted that is just an example. Essentially, I need to use the item_id from the main query inside the subquery. I could have sworn that I'd done this before, but I can't remember how...

I'm trying to do this with just a query, not with any extra programming languages. I'd like to eventually set it as a stored procedure. Thanks or any assistance with this.

UPDATE

Looks like a join did work... Thanks for all the assistance.

Here is my final query just in case anyone else runs into something like this:

SELECT DISTINCT
    Q.item_id, Q.timestamp, Q.employee,
    QA.employeeNotes, Q2.itemDesc, Q2.itemExtraData
FROM 
    itemLog Q 
LEFT JOIN 
    itemComments QA ON Q.item_id = QA.item_id 
LEFT JOIN 
    itemLog Q2 ON Q.item_id = Q2.item_id AND Q2.type = 'begin' 
WHERE 
    Q.verb = 'itemStatus' 
    AND NOT (QA.employeeNotes IS NULL);
2
  • I didn't try a join, because I figured the actual query was going to be too much for that. I'll give that a shot... Commented Jun 22, 2012 at 14:43
  • 1
    you should use an inner join with QA, since you want one of its fields not null in the where clause - Thanks for accepting my answer. Commented Jun 22, 2012 at 15:22

6 Answers 6

1
SELECT a.item_id AS mainID, b.anotherField 
FROM theMainTable a
    INNER JOIN anotherTABLE b ON a.item_id=b.item_id

You should avoid using subqueries in select statements, because they will be computed for each returned row from the main table, whereas the inner join ensures proper optimization and tables paths.

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

Comments

1

You should use

SELECT mt.itemID AS mainID, at.anotherField
FROM theMainTable mt INNER JOIN anotherTABLE at
ON mt.itemID = at.item_id

Comments

1
SELECT themaintable.item_id AS mainID, 
       anothertable.anotherfield 
FROM   themaintable 
       INNER JOIN anothertable 
               ON item_id = mainid; 

Comments

1

Try something like this:

SELECT
    item_id AS mainID,
    anotherField
FROM theMainTable
INNER JOIN anotherTABLE ON item_id=mainID;

Comments

1

Join the two tables, that's why you have keys:

SELECT table_x.id, table_y.another_field
FROM table_x
INNER JOIN table_y ON table_x.id = table2.x_id;

Comments

1

If you really need to have a nested query then do:

SELECT item_id AS mainID, 
   (SELECT anotherField 
    FROM anotherTABLE 
    WHERE anotherTABLE.item_id=theMainTable.item_id) AS anotherField
FROM theMainTable;

The reason the nested query was not working could be because you were not defining the table the field was coming from and there was column name ambiguity.

Having said the above, this really is a case where you would use a join instead:

SELECT theMainTable.mainID, 
       anotherTABLE.anotherField
FROM theMainTable INNER JOIN anotherTABLE
ON theMainTable.item_id = anotherTABLE.mainID

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.