2016-07-22 3 views
0

나는 두 개의 테이블을 가지고MySQL의 올바른 SELECT 문

첫 번째 :

things    descriptions 
------    ------------ 
First thing   First description 
Second thing   Second description 
Third thing   Third description 
Fourth thing   Fourth description 

... 

두 번째 :

id  products 
--  -------- 
1  First product 
2  Second product 
3  Third product 
4  Fourth product 

... 

은 내가에서 처음 두 행을 표시하는 질의를 필요로 첫 번째 테이블과 두 번째 테이블의 첫 번째 행, 두 번째 테이블의 두 번째 행, 두 번째 테이블의 두 번째 행 등이 얻어집니다.

things+id   prod+descr 
---------   ---------- 
First thing   First description 
Second thing   Second description 
1     First product 
Third thing   Third description 
Fourth thing   Fourth description 
2     Second product 

... 

어떻게하면됩니까? 어쩌면 유니온이 있겠 니? 고마워요!

+0

"합집합"을 몇 행이나해야합니까? – scaisEdge

+0

어떤 행이 "첫 번째"행인지 어떻게 알 수 있습니까? 또는 "처음 두"행? ORDER BY 절이 없으면 특정 행이 "첫 번째"행으로 반환된다는 보장이 없습니다. 일단 문제가 해결되면 두 문장의 결과를 "인터리브"하는 문장을 작성할 수 있습니다. 그러나 그것은 추악한 SQL을 필요로 할 것인데, 나는 이것이 SQL 문에서해야만하는 몇 가지 이유가 없다면하지 않을 것이다. – spencer7593

+0

예를 들어 본보기의 본질이 목표를 흐리게하고있을 수도 있지만 * 왜 * 이것을 원할 것입니까? – Will

답변

0

첫 번째 테이블의 PK와 일치하는 첫 번째 테이블에 다른 열을 추가하는 것이 가장 좋습니다. 이와 같은 데이터를 갖는 것이 허용 될 수 있습니까? 그런 다음 프로그램에서 검색 하시겠습니까?

t2.ID t2.prod  t1.thing  t1.desc 
------ -------  --------  ------------- 
1  first prod first thing first desc 
1  first prod second thing second desc 

UPDATE : 필요에 따라이

// setup count 
$countOuter = 0; 
$countInner = 0; 

//connect 
$mysqli = mysqli_connect(localhost,user,pass,database); 

// heres the tricky part you will have to make sure that your 
// tables are filled out at a ratio of 2:1 or there could be an 
// error thrown 

// not sure if this is going to be necessary for your purposes 
$sqlTest = "select * from tableOne" 
$sqlTest2 = "select * from tableTwo"  
$result1 = mysqli_query($mysqli, $sqlTest) or die (mysqli_error($mysqli)); 
$result2 = mysqli_query($mysqli, $sqlTest2) or die (mysqli_error($mysqli)); 
$rowsTableOne = mysqli_num_rows($result1); 
$rowsTableTwo = mysqli_num_rows($result2); 

// check ratio 
if(($rowsTableOne/$rowsTableTwo) == 2) 
{ 

    while($countOuter < $rowsTableOne) 
    { 
     //outer query 
     $sqlOuter = "select * from tableOne LIMIT 2"; 
     if ($count % 2 == 0) { 
     $sqlOuter .= " OFFSET ".$count; 
     } 
     $result = mysqli_query($mysqli, $sqlOuter) or die (mysqli_error($mysqli));  
     while($row = mysqli_fetch_array($result, MYSQLI_NUM)) 
     {   
      echo "<p>".$row[0]."&nbsp;".$row[1]."</p>"; 
      $countOuter++; 
     } 

     $sqlInner = "select * from tableTwo LIMIT 1"; 
     if ($countInner != 1) { 
     $sqlInner .= " OFFSET ".$countInner; 
     } 
     $result = mysqli_query($mysqli, $sqlInner) or die (mysqli_error($mysqli));  
     while($row = mysqli_fetch_array($result, MYSQLI_NUM)) 
     {   
      echo "<p>".$row[0]."&nbsp;".$row[1]."</p>"; 
      $countInner++; 
     } 
    } 
} 

이 나는 ​​그것을 테스트 할 시간이 없었다하지만 일반적으로 방향을 가리켜 야합니다, 당신에게 몇 가지 일반적인 아이디어를 제공해야합니다.

+0

두 번째 테이블에는 제품을 화면에 두는 것을 알리는 제품이 있습니다 첫 번째 표의 줄 사이. 사용자가 첫 번째 테이블의 레코드 중에서 검색을하고 두 번째 결과를 첫 번째 테이블에 표시하려고합니다. PHP를 사용하여이 작업을 수행합니다. – Skie

+0

어떤 종류의 프로그램을 사용하고 있습니다. 즉, 데이터를 처리하는 데 사용하는 언어는 무엇입니까? 사용자가 SQL을 알기를 기대하고 있습니까? 아니면 C# 프로그램 인 PHP를 사용하고 있습니까? – Danimal

+0

그러면 첫 번째 테이블에 fk를 만드는 것이 당신이해야 할 일의 무결성을 보존하는 가장 좋은 방법입니다. 당신은 귀하의 PHP mysqli 루프 – Danimal