我想知道从2家商店租来的人数。
架构如下。
final Cursor cursor = myDb.cautarevenituri();
// The desired columns to be bound
final String[] columns = new String[] {
DatabaseHelper.COL_2,
DatabaseHelper.COL_3
};
int[] toviewids = new int[] { R.id.nume_item,R.id.valoare_item};
dataAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(),R.layout.item_layout,cursor,columns,toviewids,0);
//
final ListView listView = (ListView)getView().findViewById(R.id.listView_venituri);
listView.setAdapter(dataAdapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long arg3) {
Toast.makeText(getActivity(), " " + position, Toast.LENGTH_LONG).show();
//delete from listview in database and listview too??
//
//
return false;
}
});
通过此查询,我可以获得从2家商店租借的客户列表。
Rental table
----------------------------------------------------
rental_id int(11) NO PRI
rental_date datetime NO MUL
inventory_id mediumint(8) unsigned NO MUL
customer_id smallint(5) unsigned NO MUL
return_date datetime YES
staff_id tinyint(3) unsigned NO MUL
last_update timestamp NO CURRENT_TIMESTAMP
Inventory table
---------------------------------------------------
inventory_id mediumint(8) unsigned NO PRI
film_id smallint(5) unsigned NO MUL
store_id tinyint(3) unsigned NO MUL
last_update timestamp NO CURRENT_TIMESTAMP
如何将计数作为同一sql语句的一部分来查找?
答案 0 :(得分:0)
您将使用两个聚合。第一个是从两个商店租来的人。第二个将它们加起来。所以:
select count(*)
from (select r.customer_id
from rental r join
inventory i
on r.inventory_id = i.inventory_id
group by r.customer_id
having count(distinct i.store_id) = 2
) r;
答案 1 :(得分:-1)
select count(*) from(
select customer_id
from rental r , inventory i
where r.inventory_id = i.inventory_id
group by customer_id
having count(distinct i.store_id) = 2);
你必须在内部查询中汇总你的结果,所以你必须用另一个计算所有结果的查询来说明查询。
答案 2 :(得分:-2)
select r.customer_id, count(*)
from rental r
join inventory i
on r.inventory_id = i.inventory_id
where i.store_id = 2
group by r.customer_id