Question

Database query problem

the questions 2,5 and 6 of this sample paper

question 2 is not written properly, it should be product name , supplier name which supplies maximum quantity of each productProduct Supplier prid prname price (int) (int) vc(10) PK 101 Scale 104 Sketch 110 105Tape 107 Pad 110 Pad 112Pin 124 Pin 135

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Qo.

ANS:

Let's Create DB first and their structure and then join tables them by INNER JOIN.

Product Databse1 Structure: IMAGE:

Server: 127.0.0.1>Database: stationaryTable: product 目Browse И Structure gSQL → Search Insert -Export -Import na Privileges /

DATA IMAGE:

HT- prid prnameprice Edit CopyDelete 101 Scale 30 110 Edit i Copy Delete 105 Tape 15 160 25 5 120 110 Edit e Copy Delete 104

Supplier Databse2 Structure: IMAGE:

Server: 127.0.0.1> Database: stationaryTable: supplier Browse И structure!目SQL Search Insert -Export | 몬 Import .n Privileges

DATA IMAGE:

Server: 127.0.0.1 » Database: stationaryTable: supplier 目Browse yt Structure LjSQL 、Search Insert Show BLOB conte supid supna

Supply Databse3 Structure: IMAGE:

Server 1270 01 ® Database stationary 》圜Table supply ■ Browse Bt Structure □SQL → Search Insert -Export H Import nl Privileges

DATA IMAGE:

Server: 127.0.0.1 » Database: stationaryTable: supply Br O Full texts O Display column for relationships Structure !」SQL Sear
Now comes to query(Questions)

NOTE: I am using PHP MySQL DB, but queries are same in SQL DB, So focus on the query rather than medium).

Qo2: get the product name, supplier name which supplying maximum quantities of each product.

ANS:
QUERY IS: "SELECT prid,prname,supname,qty FROM product INNER JOIN supply on product.prid=supply.pid INNER JOIN supplier on supplier.supid=supply.sid order by qty desc";

PHP CODE IS:

<html>
<head>
   <title>Show Data</title>
   <style>
       table{
           border-collapse: collapse;
           width: 100%;
       }
       tr,td{
           padding: 15px;
           text-align: center;
       }
   </style>
</head>
<?php
   $product_db=mysqli_connect("localhost","root","","stationary") or die("Can't connect to db");
   $product_query="SELECT prid,prname,supname,qty FROM product INNER JOIN supply on product.prid=supply.pid INNER JOIN supplier on supplier.supid=supply.sid order by qty desc";
   $product_result=mysqli_query($product_db,$product_query);
   echo "<table border='1'>";
   echo "<tr>
           <th>Product ID</th>
           <th>Product Name</th>
           <th>Supplier Name</th>
           <th>Quantity(s)</th>
       </tr>";
   while($product_ans=mysqli_fetch_assoc($product_result))
   {
       echo "<tr>
               <td>".$product_ans['prid']."</td>
               <td>".$product_ans['prname']."</td>
               <td>".$product_ans['supname']."</td>
               <td>".$product_ans['qty']."</td>
           </tr>";
      
   }
   echo "</table>";
?>

you can find max using max(qty).

OUTPUT IMAGE:

Product ID Product Name Supplier Name Quantity(s) 107 Pad Topper 104 Sketch 420 Rally 104 Sketch 380 105 Tape 350 124 107 Pad

Q5) write a function that takes the product id as input and return supplied quantity.

ANS: Function is:

function getQuantity($prod_id)
   {
       $product_db=mysqli_connect("localhost","root","","stationary") or die("Can't connect to db");
       $product_query="SELECT * FROM product INNER JOIN supply on product.prid=supply.pid where prid=$prod_id";
       $product_result=mysqli_query($product_db,$product_query);
       $product_ans=mysqli_fetch_assoc($product_result);
       $totalQty=$product_ans['qty'];
       return $totalQty;
   }
  
   echo "Quantity is: ".getQuantity(104).'<br/>';
   echo "Quantity is: ".getQuantity(105).'<br/>';

Qo6)

ANS: Function with PHP Code is:

<html>
<head>
   <title>Show Data</title>
   <style>
       table{
           border-collapse: collapse;
           width: 100%;
       }
       tr,td{
           padding: 15px;
           text-align: center;
       }
   </style>
</head>
<?php
  
   function getQuantity($prod_id)
   {
       $product_db=mysqli_connect("localhost","root","","stationary") or die("Can't connect to db");
       $product_query="SELECT * FROM product INNER JOIN supply on product.prid=supply.pid INNER JOIN supplier on supplier.supid=supply.sid where prid=$prod_id ";
       $product_result=mysqli_query($product_db,$product_query);
      
       if(mysqli_num_rows($product_result)>0)
       {
           echo "<table border='1'>";
               echo "<tr>
                   <th>Product ID</th>
                   <th>Product Name</th>
                   <th>Supplier Name</th>
                   <th>Quantity(s)</th>
               </tr>";
           while($product_ans=mysqli_fetch_assoc($product_result))
           {
               echo "<tr>
                       <td>".$product_ans['prid']."</td>
                       <td>".$product_ans['prname']."</td>
                       <td>".$product_ans['supname']."</td>
                       <td>".$product_ans['qty']."</td>
                   </tr>";
              
           }
           echo "</table>";
       }
       else{
           echo "No Product Supplied";
       }
   }
       getQuantity(104);
       getQuantity(105);
?>

OUTPUT IMAGE:

Supplier Name Product ID Product Name Quantity(s) ID 104 Sketch Avon 420 104 Product ID 105 Sketch Product Name Tape Rall Sup---------------------------------------------------------------------------------------------------------------------------------------------------------------------

THANKS!!

Add a comment
Know the answer?
Add Answer to:
Database query problem the questions 2,5 and 6 of this sample paper question 2 is not written properly, it should be product name , supplier name which supplies maximum quantity of each product Produc...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT