2013-04-10 2 views
0

이렇게 처음으로 INNER JOIN 문에 제대로 접근하고 있으며, 잘 진행되지 않는다고 말할 필요는 없습니다.내부 결합 및 PHP

  • 명령서이 전에 페이지에서 제출 : 기본적으로 내가 할 노력하고 무엇

    <?php require_once('Connections/reps.php'); ?> 
    <?php 
    
    if (!function_exists("GetSQLValueString")) { 
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    { 
        if (PHP_VERSION < 6) { 
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 
        } 
    
        $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); 
    
        switch ($theType) { 
        case "text": 
         $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
         break;  
        case "long": 
        case "int": 
         $theValue = ($theValue != "") ? intval($theValue) : "NULL"; 
         break; 
        case "double": 
         $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; 
         break; 
        case "date": 
         $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
         break; 
        case "defined": 
         $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; 
         break; 
        } 
        return $theValue; 
    } 
    } 
    
    mysql_select_db($database_reps, $reps); 
    $query_orders = "SELECT * FROM orders"; 
    $orders = mysql_query($query_orders, $reps) or die(mysql_error()); 
    $row_orders = mysql_fetch_assoc($orders); 
    $totalRows_orders = mysql_num_rows($orders); 
    
    mysql_select_db($database_reps, $reps); 
    $query_staff = "SELECT * FROM staff"; 
    $staff = mysql_query($query_staff, $reps) or die(mysql_error()); 
    $row_staff = mysql_fetch_assoc($staff); 
    $totalRows_staff = mysql_num_rows($staff); 
    
    mysql_select_db($database_reps, $reps); 
    $query_products = "SELECT * FROM products"; 
    $products = mysql_query($query_products, $reps) or die(mysql_error()); 
    $row_products = mysql_fetch_assoc($products); 
    $totalRows_products = mysql_num_rows($products); 
    
    mysql_select_db($database_reps, $reps); 
    $join = "SELECT 
    orders.ID AS id, 
    orders.CustomerName AS customerName, 
    orders.StaffMember AS staffMember, 
    orders.SalesPrice AS salesPrice, 
    orders.ListPrice AS listPrice, 
    orders.AgeOfPayment AS productAge, 
    orders.Product AS productName, 
    orders.Quantity AS orderQuantity, 
    orders.DateOfOrder AS orderDate, 
    staff.Bonus AS staffBonus, 
    products.Price AS productPrice 
    FROM 
    orders AS orders 
    INNER JOIN staff AS staff ON orders.StaffMember = staff.StaffName 
    INNER JOIN products AS products ON orders.Product = products.ProductName 
    ORDER BY 
    orderDate ASC"; 
    
    $joinresult = mysql_query($join); 
    
    ?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title></title> 
    <?php 
    
    
    require_once("models/config.php"); 
    if (!securePage($_SERVER['PHP_SELF'])){die();} 
    require_once("models/header.php"); 
    
    ?> 
    </head> 
    
    <body> 
    <div id='wrapper'> 
    <div id='top'> 
        <div id='logo'></div> 
    </div> 
    <div id='content'> 
        <h1></h1> 
        <h2>Change This</h2> 
        <div id='left-nav'> 
        <?php include("left-nav.php"); ?> 
        </div> 
        <div id='main'> 
        <div id ="orderstable"> 
         <table border="1" cellpadding="1" cellspacing="3"> 
         <tr> 
          <th>Order ID</th> 
          <th>Customer Name</th> 
          <th>Product</th> 
          <th>Quantity</th> 
          <th>Sales Price (€)</th> 
          <th>Total Sale (€)</th> 
          <th>List Price (€)</th> 
          <th>Price Difference (€)</th> 
          <th>% Difference</th> 
          <th>Sold By</th> 
          <th>Order Date</th> 
          <th>Age of Payment</th> 
          <th>Bonus %</th> 
          <th>Bonus Amount</th> 
         </tr> 
         <?php do { ?> 
         <?php do { ?> 
          <tr> 
          <td align="center" valign="middle"><?php echo $row_orders['ID']; ?></td> 
          <td align="center" valign="middle"><?php echo $row_join['customerName']; ?></td> 
          <td align="center" valign="middle"><?php echo $row_join['product']; ?></td> 
          <td align="center" valign="middle"><?php echo $row_orders['Quantity']; ?></td> 
          <td align="center" valign="middle"><?php echo $row_orders['SalesPrice']; ?></td> 
          <td align="center" valign="middle"><?php $first_multiply = $row_orders['Quantity']; 
                   $second_multiply = $row_orders['SalesPrice']; 
                   $multiply_total = $first_multiply * $second_multiply; 
                   echo ($multiply_total); ?></td> 
          <td align="center" valign="middle"><?php echo $row_orders['Price']; ?></td> 
          <td align="center" valign="middle"><?php $first_number = $row_orders['Price']; 
                   $second_number = $row_orders['SalesPrice']; 
                   $sum_total = $first_number - $second_number; 
                   echo ($sum_total); ?></td> 
          <td align="center" valign="middle">as</td> 
          <td align="center" valign="middle"><?php echo $row_join['staffMember']; ?></td> 
          <td align="center" valign="middle"><?php echo date('d/m/Y', $row_orders['orderDate']); ?></td> 
          <td align="center" valign="middle"></td> 
          <td align="center" valign="middle"><?php echo $row_orders['bonus']; ?>%</td> 
          <td align="center" valign="middle">&nbsp;</td> 
          </tr> 
          <?php } while ($row_orders = mysql_fetch_assoc($orders));?> 
          <?php } while ($row_join = mysql_fetch_array($join));?> 
         </table> 
        </div> 
        </div> 
        <div id='bottom'></div> 
    </div> 
    </body> 
    </html><?php 
    mysql_free_result($orders); 
    
    mysql_free_result($staff); 
    
    mysql_free_result($products); 
    ?> 
    

    은 다음과 같다 : 다음은 내 페이지 코드입니다. 데이터는 데이터베이스의 테이블에 저장되고 에코됩니다.

  • 해당 테이블 세 개가 staff, orders 및 products 테이블입니다.
  • 값이 아닌 ID를 기반으로 양식을 저장했습니다. 내가 봤 거든 연구하고 INNER JOIN과 나는 작동하지 않는 아래에 노력했다.

내가 옳은 방향으로 나를 잘못 지적 할 수있는 사람은 누구입니까?

답변

0
SELECT 
o.ID, 
o.CustomerName, 
o.StaffMember, 
o.SalesPrice, 
o.ListPrice, 
o.AgeOfPayment, 
o.Product, 
o.Quantity, 
o.DateOfOrder, 
s.Bonus, 
p.Price 
FROM 
`orders` o 
INNER JOIN staff AS s ON o.StaffMember = s.StaffName 
INNER JOIN products AS p ON o.Product = p.ProductName 
WHERE o.StaffMember = s.StaffName 
AND o.Product = p.ProductName 
ORDER BY 
orderDate ASC"; 

그러나 테이블이 잘 구성되어 있지 않은 느낌이 들었습니다. Product = ProductName 대신 ID를 사용하는 것이 좋습니다.

+0

완벽하고, 그 친구에게 감사드립니다. –

0

나는 이런 식으로 가입 할 것 :

$join = "SELECT o.ID id, 
       o.CustomerName customerName, 
       o.StaffMember staffMember, 
       o.SalesPrice salesPrice, 
       o.ListPrice listPrice, 
       o.AgeOfPayment productAge, 
       o.Product productName, 
       o.Quantity orderQuantity, 
       o.DateOfOrder orderDate, 
       s.Bonus staffBonus, 
       p.Price productPrice 
     FROM  
       orders o, 
       staff s, 
       products p 
     WHERE 
       o.StaffMember = s.StaffName 
     AND 
       o.Product = p.ProductName 
     ORDER BY 
       orderDate"; 

3 개 테이블을 조인 할 때이 조건이 있기 때문에, 모든 결과를 얻을 수 있습니다. 다른 AND을 넣어 orders 테이블에 고유 한 것을 제시하고 특정 주문에 대한 정보를 얻을 수 있습니다.

0

주목할 점은 mysql_ 함수가 PHP에서 더 이상 사용되지 않는다는 것입니다. mysqli_ (mysql_ 클래스와 매우 유사하다) 또는 PDO로 전환 할 수있다.