Question : Get a distinct result from three different tables

I have three tables which are accounts, Balance and customer (please see the table structure in the code section below) and I am trying to get distinct information from it without any particular customer repeating - here are the three statements I want to achieve and the statements I have tried to use:

1.  I want to get all customers who have reached the 75% threshold:-

     Select ((b.total / Limiter)*100) as perc, b.total, fname, lname from customer c, balance b WHERE c.status = 1 AND b.cid = c.id and ((b.total / Limiter)*100) < -75

This gets the customers, but if for instance Mark has reached 75 on his first transaction and on his second reaches 85% then the above statement is printing Mark two times, one with 75% and one with 85% when all I want is Mark to be listed once with the max percentage - 85% in this case.

2. This time I want to get customers whose due date is within 7 days

      Select dateFrom, aid, min, mout, bal, dateTo, fname, lname from accounts a, customer c WHERE  c.status = 1 and  dateTO BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 0 DAY ) AND DATE_ADD( CURDATE( ) ,INTERVAL 4 DAY ) and id = cid

The statement above does almost what I want, however just like question 1 it repeats customers and their information when All that I want to display is the name, last balance and the due date.

Many thanks for your time.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
`accounts` (
  `aid` int(11) NOT NULL AUTO_INCREMENT,
  `cid` int(11) NOT NULL,
  `mout` int(11) DEFAULT NULL,
  `min` int(11) DEFAULT NULL,
  `bal` int(11) NOT NULL,
  `dateFrom` date NOT NULL,
  `dateTo` date DEFAULT NULL,
  PRIMARY KEY (`aid`)
)

`balance` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cid` int(11) NOT NULL,
  `total` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `aid` (`cid`)
)

`customer` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fname` varchar(200) NOT NULL,
  `lname` varchar(200) NOT NULL,
  `tel` varchar(20) NOT NULL,
  `Address` text NOT NULL,
  `Limiter` int(11) NOT NULL,
  `status` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`)
)

Answer : Get a distinct result from three different tables

File are sequential so unless the lines are FIXED WIDTH in length and you can calculate the byte offset of XXX line number, then your only option is to COUNT line by line one at a time or by reading in "chunks" and splitting on carriage return/line feed.  Either option still requires you to physically count the lines until you reach the target.
Random Solutions  
 
programming4us programming4us