How to count ID in other table? |
[eluser]sovandy[/eluser]
Dear Codeigniter, I have a problem and I want you to help me now. I want to count boo_id in table lib_borrowings. I show you about my table: lib_books ---------------- |<b>boo_id | boo_title</b>| |--------------- 1 | AAAA ---------------- 2 | BBBB ---------------- 3 | CCCC ---------------- 4 | DDDD ---------------- lib_borrowings --------------- |<b>bor_id | boo_id</b>| -------------- 1 | 1,2,3 -------------- 2 | 2,3,4 -------------- I want to count the boo_id in table lib_borrowings because I want to know how many books that are borrowed. Everybody please help me with this problem. I've tried to do it since yesterday.
[eluser]kgill[/eluser]
Given the ugly table design (storing the borrowed books as a string of ID's instead of as rows) you can't do it via a query, instead you have to select the list and process it with PHP - look at the explode function.
[eluser]depthcharge[/eluser]
Code: Everybody please help me with this problem. I’ve tried to do it since yesterday. Save yourself a lot of future problems and re-design the database schema, storing data like this is going to make your work harder.
[eluser]sovandy[/eluser]
Oh one more requirement! If I want to count each unique of array(), what is the easy function to use? for example, 32 has 4 times in boo_id 20 has 2 times in boo_id Thank,
[eluser]sl3dg3hamm3r[/eluser]
As long as you won't have a proper normalization of your model, you shouldn't even start to think about such issues. You will end up in a nightmare...
[eluser]jedd[/eluser]
Seriously, dump your current schema - and come up with a new one. Go read the [url="http://en.wikipedia.org/wiki/Database_normalisation"]wikipedia article on database normalisation[/url] before you start. |
Welcome Guest, Not a member yet? Register Sign In |