开发者

Result display problem in php and mysql

        A                   B                   C

P_id | nam开发者_StackOverflow社区e | add     P_id  |  t_id        t_id |  paper  
-----------------     --------------     ------------------ 
  1   sam    ca         1        1           1      asdxa  
  2   john   de         2        1           2      dgfv  
  3   sam    jk         3        2           3      decgf  
  4   sam    ca         4        3           4      ergvtr

Now I can easily make a search for Name 'sam' in Table A group by name, add. and It shows me result like,

sam ca

sam jk

I am using php and mysql. I want to do some additional task in this:-

--> Totalcount for the rows..Like

sam ca 2

sam jk 1

(I am not taking P_id here... just focus on name and add.)

--> Make a link on 2 and 1 (above). so if I click on it. It should displays the related papers on another html page from table C. example: if I click on 2... then it should display asdxa and decgf.


--> Totalcount for the rows..Like

SELECT `name`, `add`, count(`add`) FROM `A` WHERE (`name` = 'sam') GROUP BY `add`;

As for the linking, you just link to the page with that ID, and have your php script get the data from the given id in the C table.

To select all matching occurences while searching for a name:

SELECT  `A`.`name` AS name,  `A`.`P_id` AS aid,  `C`.`t_id` AS cid,  `C`.`paper` AS paper
FROM  `A` , `C` 
WHERE (
    `A`.`name` =  'sam'
)
AND (
    `A`.`P_id` =  `C`.`t_id`
)

result:

name    aid cid paper
sam     1   1   qwertyui
sam     3   3   zxcvbn
sam     4   4   uytrewq
sam     5   5   hfdsa

Which matches the test tables I did in my local environment


Try to use for the first task

$pdo = new PDO(....);
$result = $pdo->query('SELECT name, add, count(*) from table where name='sam' group by add;')->fetchAll();
if ($result) {
    foreach($result as $row) {
         ......
    }
}
//second
$sql = 'select paper from tableA a inner join TableB b on a.P_id=b.P_id inner join Tablec  c inner join b.t_id = c.t_id where a.p_id=2';

//repeat previous statements

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜