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
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
Try this query.
SELECT * FROM 'table_name' where out=0;
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
Related
I have two tables and I need to get the count of these tables for a condition?
table 1: "tbl_comment" table 2: "tbl_group_comment"
both have some common columns which are login_id of the user,
this is the query:
$param="(SELECT COUNT(*) FROM tbl_comment as t2 WHERE login_id=1) as commentCount, (SELECT COUNT(*) FROM tbl_group_comment as t3 WHERE login_id=1) as commentCount";
$table="`tbl_comment` t2 join `tbl_group_comment`";
$save['value']=$this->Common_model->common_join($param,$table);
this one is not working but if I am changing the second commentCount and keep it as commentCount1 it will give me the value of each table, I want to get the sum of both counts?
is there any specific clause for this matter?
Try this:
select sum(comment_count + group_count) from
((SELECT COUNT(*) as comment_count FROM tbl_comment WHERE login_id=1) A,
(SELECT COUNT(*) as group_count FROM tbl_group_comment WHERE login_id=1)B
);
I have a main table: Images and votes.
The votes contains 2 fields: Id, image_id
What I wish to do is query the images table and return them ordered by the amount of votes in the vote table.
Currently:
$stmt = $conn->prepare(
'SELECT *
FROM images
LIMIT 10'
);
I understand basic left joins, but cannot think how to order them by count of votes. Just need a starting example.
Thanks
Assuming that each row in your VOTES table means one vote towards the image, this should work: SQL DEMO
SELECT *, COUNT(v.image_id)
FROM images AS i
LEFT JOIN votes AS v ON i.id=v.image_id
GROUP BY i.id
ORDER BY v.image_id desc
drop table if exists t1;
create table t1 (id int);
insert into t1 values (1), (2), (3);
drop table if exists t2;
create table t2 (id int, img_id int);
insert into t2 values (1, 1), (2, 1), (3, 2), (4,1), (5,3), (6,2);
select *
from t1
left join
(select img_id, count(*) count
from t2
group by img_id) c
on img_id=t1.id
order by count
Demo on sqlfiddle
I have Problems with a select statement, as a little help here are the important columns:
Table1
ID NAME
TABLE 2
ID U_ID COUNTER
The ID of Table 1 Matches the U_ID of Table 2. Table 2 contains many entries for the same u_id.
What I want to do is to get the Name of the "user" (table 1) who has in sum the max. counter.
What I got since now is the join of the tables (Where clause depends on other rows which are not important for the problem).
Can anyone help me on this issue?
So what you need is an aggregate of an aggregate (max of sum of column). The easiest will be to create a view providing the sum and u_id end then select the max of it:
create view table2sums
as
select u_id, sum(counter) as total
from table2
group by u_id;
and then
select t1.name
from table1 t1, table2sums t2
where t1.id = t2.u_id
and t2.total >= all (
select total
from table2sums
)
In this special case you can also do it directly:
select t1.name
from table1 t1, table2 t2
where t1.id = t2.u_id
group by t1.name
having sum(t2.counter) >= all (
select sum(counter)
from table2
group by t2.u_id
)
NOTE: The other proposed solutions will show a better performance. My solution only selects the name (which is what you said you wanted) and works in any RDBMS.
There exist RDBMS without the LIMIT possibility.
In the end, I'd say: regard my solution as educational, the others as practical
SELECT name,
SUM(counter) as counter
FROM table1
JOIN table2
ON table1.id = table2.u_id
GROUP BY u_id
ORDER BY counter DESC
LIMIT 1
You can try this:
SELECT name, SUM(counter) as total_counter
FROM table1
JOIN table2
ON table1.id = table2.u_id
GROUP BY u_id
ORDER BY total_counter DESC
LIMIT 1
Working Demo: http://sqlfiddle.com/#!2/45419/4
I have two tables Table1 and Table2 with some records
id is the common column in both tables and primarykey is set to this column in table1
There are many records in table1 and some of these records (not all) are updated into table2.
Now I want retrieve from table1 the records not updated into the table2.
For example in table1 there are records 1,2,3,4,5,6,7,8,9
And in table2 there are 3,4,7,9
Now How can I retrieve these records form table1 1,2,5,6 those not updated into table2
I wrote this query :
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME
FROM [Table1] INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id;
But the expected result not coming. This query lists all the records repeating each one record manytimes
Can any body give me solution to get the expected result.
select *
from table1
where table1.slip_no NOT IN (select id from table2)
Assuming name of common column is id
Or you can modify your query as
SELECT distinct (Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME)
FROM [Table1]
INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id
A good reference on SQL joins
SELECT t1.*
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2 USING(id)
WHERE
t2.id IS NULL;
You can use the NOT IN operator on a subquery for table2.
Alternatively, use MINUS with two regular queries listing the ids in each table:
SELECT id FROM table1
MINUS
SELECT id FROM table2;
Try this
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME FROM [Table1]
WHERE
NOT EXISTS (SELECT * from Table2 WHERE Table1.SLIPNO !=Table2.id );
You can use the following query
SELECT id FROM database1.table WHERE id NOT IN(SELECT id FROM database2.table)
SELECT child_table.id FROM child_table LEFT JOIN parent_table ON child_table.parent_id = parent_table.id WHERE parent_table.id IS NULL
This left join query returns all the records of the child_table when there is no match in the parent_table. When there is no match, all parent_table fields will be NULL.
inner join will not help. To get unmatched records I tried this:
SELECT
A.ID,A.DATE,A.NAME
FROM TABLE1 A
WHERE CONCAT(A.ID , A.DATE ,A.NAME)
NOT IN
(SELECT CONCAT(B.ID , B.DATE ,B.NAME) as X
from TABLE2 B) ;
I have two tables.
Structure of first table:
id
secondtableid
value
Structure of second table:
id
title
How can I select fields 'id' from first table know value of 'value' column and order result by value of 'title' column of second table, if secondtable id is id of second table?
You can order by fields you don't select.
select FirstTable.id, FirstTable.value
from FirstTable
inner join SecondTable on (FirstTable.SecondTableID=SecondTable.ID)
order by SecondTable.Title
:)
select id from first_table
inner join second_table on first_table.secondtableid = second_table.id
where value = 'Known Value'
order by second_table.title
Something like this:
select
firsttable.id, firsttable.value, secondtable.title
from
firsttable
join
secondtable on firsttable.secondtableid = secondtable.id
order by
secondtable.title
This a beginner question, so I will answer as simple as possible:
SELECT t1.id, t1.value
FROM table1 t1
INNER JOIN table2 t2
ON t1.secondtableid = t2.id
ORDER BY t2.title
You can JOIN the tables, indicating how they join.