MySql选择数据是PHP中的多对多关系

时间:2013-02-12 03:52:30

标签: php mysql database random relationship

  • ENTRY(表)

    ent_seq  entry
    100010      1
    100020      2
    100030      3
    100040      4
    
  • K_ELE(表)

    KID  entry   keb 
    1        1   食べる
    2        2   日本
    3        3   先生
    
  • R_ELE(表)

    RID  entry   reb 
    1        1   たべる
    2        2   にほん
    3        2   にっぽん
    4        3   せんせい
    5        4   ビール
    
  • SENSE(表)

    SID  entry
    1        1
    2        2
    3        3
    4        4
    
  • GLOSS_ENG(SENSE表的子表)

    GID SID  mean 
    1     1  eat
    2     2  Japan
    3     2  Japanese
    4     3  theacher
    5     4  beer
    
  • 更新(SENSE表的子表)

     UPID   SID    up_date 
      1     1    2012.12.12 17:31
      2     2    2012.12.12 17:31
      3     2    2012.12.12 17:31
      4     3    2012.12.12 17:31
      5     4    2012.12.12 17:31
    

我喜欢这张表。

   ent_seq  keb     reb            mean             update

   100010   食べる   たべる           eat             2012.12.12 17:31
   100020   日本    にほん; にっぽん   Japan; Japanese 2012.12.12 17:31
   100030   先生    せんせい          theacher        2012.12.12 17:31
   100040           ビール   beer                    2012.12.12 17:31

这个数据库非常大195000行请帮助我!!!

我的简单代码KKK

 <?php include('con_db.php');
$query = mysql_query("SELECT * FROM entry");
$number=mysql_num_rows($query);
for ($count=1; $count<= 20 ; $count++)
{ $entry = RAND(1,$number);

$query1 = "SELECT   
        entry.ent_seq, k_ele.keb, r_ele.reb, s_gloss_eng.gloss
        FROM entry, k_ele, r_ele, sense, s_gloss_eng
        WHERE entry.entry_pkey = $entry
        AND   sense.sense_pkey = s_gloss_eng.sense_pkey
        AND   entry.entry_pkey = sense.entry_pkey
        AND   entry.entry_pkey = r_ele.entry_pkey
        AND   entry.entry_pkey = k_ele.entry_pkey
        ORDER BY entry.ent_seq
        ";
$view2 = mysql_query($query1);
$row = mysql_fetch_assoc($view2);

    $ent_seq=$row['ent_seq'];
    $keb=$row['keb'];
    $reb=$row['reb'];
    $mean_eng=$row['mean'];
    $update=$row['update'];
?>

      <table width="614" border="1">
   <tr>
     <td>ent_seq</td>
     <td>keb</td>
     <td>reb </td>
     <td>mean</td>
     <td>update</td>
  </tr>
  <tr>
     <td>$ent_seq</td>
     <td>$keb</td>
     <td>$reb</td>
     <td>$mean_eng</td>
     <td>$update</td>
  </tr>
  </table>
 <?php
 }
 ?>

2 个答案:

答案 0 :(得分:0)

尝试此查询 -

SELECT
  e.ent_seq,
  e.entry,
  ke.keb,
  re.reb,
  s.mean,
  s.up_date
FROM entry e
LEFT JOIN (SELECT entry, GROUP_CONCAT(keb) keb FROM k_ele GROUP BY entry) ke
  ON ke.entry = e.entry
LEFT JOIN (SELECT entry, GROUP_CONCAT(reb) reb FROM r_ele GROUP BY entry) re
  ON re.entry = e.entry
LEFT JOIN (
  SELECT s.entry, mean, up_date FROM sense s
    LEFT JOIN (SELECT sid, GROUP_CONCAT(mean) mean FROM gloss_eng
               GROUP BY sid
              ) ge
      ON ge.sid = s.sid
    LEFT JOIN (SELECT sid, GROUP_CONCAT(DISTINCT up_date) up_date FROM `update`
               GROUP BY sid
              ) u
      ON u.sid = s.sid
  GROUP BY s.sid
  ) s
  ON s.entry = e.entry

答案 1 :(得分:0)

SELECT e.ent_seq, 
    GROUP_CONCAT(DISTINCT kei.keb SEPARATOR'; ')AS kebs, 
    GROUP_CONCAT(DISTINCT rei.reb SEPARATOR '; ') AS rebs,
    GROUP_CONCAT(DISTINCT sei.mean SEPARATOR '; ') AS means

FROM entry e

LEFT JOIN k_ele kei ON e.entry = kei.entry
LEFT JOIN r_ele rei ON e.entry = rei.entry
LEFT JOIN sense se ON e.entry = se.entry 
LEFT JOIN s_gloss_eng sei ON se.SID = sei.SID

WHERE e.ent_seq IN (SELECT jt5.jlpt5_seq FROM jlptn5 jt5
                   WHERE jt5.jlpt5_null = 0
                   ORDER BY RAND())
GROUP BY e.entry    
ORDER BY RAND()
LIMIT 20