Welcome Guest, Not a member yet? Register   Sign In
How to count ID in other table?
#1

[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 &nbsp; &nbsp;&nbsp;|&nbsp; AAAA
----------------
2 &nbsp; &nbsp;&nbsp; |&nbsp; BBBB
----------------
3 &nbsp; &nbsp;&nbsp; |&nbsp; CCCC
----------------
4 &nbsp; &nbsp;&nbsp; |&nbsp; DDDD
----------------


lib_borrowings
---------------
|<b>bor_id | boo_id</b>|
--------------
1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp; 1,2,3
--------------
2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp; 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.
#2

[eluser]Thorpe Obazee[/eluser]
$this->db->count_all();
#3

[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.
#4

[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.
#5

[eluser]Thorpe Obazee[/eluser]
lol. I didn't even look at the table design.
#6

[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,
#7

[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...
#8

[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.




Theme © iAndrew 2016 - Forum software by © MyBB