php - Selecting specific rows from table

764

I have the below table (in picture) which is kind of inventory table and shows how many items comes in and how many goes out from stock, and item_id is the foreign key from another table.

I want to select those records that has no out from the stock, in other word i want to select those records which are highlighted in green (in the picture).

Thanks.

Sorry for poor English

The Table

enter image description here

923

Answer

Solution:

Try this:

Select * from `table` where id in (select id from `table`group by id having sum(out)=0);

for deleting those values use:

delete t1
from `your_table` as t1 
join (select item_id from `your_table`group by item_id having sum(item_out)=0) t2 on t1.item_id = t2.item_id
715

Answer

Solution:

Try this query.

SELECT * FROM 'table_name' where out=0;
197

Answer

Solution:

You need to join the table to itself:SELECT t.* FROM <your_table> AS t LEFT JOIN <your_table> AS t1 ON t.item_id=t1.item_id WHERE t1.out>0 AND t1.item_id IS NULL

People are also looking for solutions to the problem: php - Do I need to secure a form that is not connected to a database

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.