I have a subject and a departments table, Each subject is associated with departments table. I am trying to select all subjects including departments name. The code bellow work perfect but show only two records. Any Help will be appreciated
//Select statement
$selects=$connection->query("SELECT
subjects.id
, subjects.name
, subjects.related_to
, subjects.related_to_sem
, departments.dept
FROM subjects
INNER JOIN departments
ON subjects.related_to = departments.dep_id");
<table class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>
<th>Sub Id</th>
<th>Subject Name</th>
<th>Related to Department</th>
<th>Related to Semester</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
while($result=$select->fetch_assoc()) {
?>
<tr>
<td><?php echo $result['id']; ?></td>
<td class="center"><?php echo $result['name']; ?></td>
<td class="center"><?php echo $result['dept']; ?></td>
<td class="center"><?php echo $result['related_to_sem']; ?></td>
<td class="center">
<a class="btn btn-info" href="#">
<i class="icon-edit icon-white"></i>
Edit
</a>
<a class="btn btn-danger" href="#">
<i class="icon-trash icon-white"></i>
Delete
</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
select
subjects.id,subjects.name,
subjects.related_to,
subjects.related_to_sem,
departments.dept
from
subjects
LEFT OUTER JOIN departments ON subjects.id=departments.dep_id
Please try the following. I think you may have used the wrong fields in the join condition:
SELECT
subjects.id
, subjects.name
, subjects.related_to
, subjects.related_to_sem
, departments.dept
FROM subjects
INNER JOIN departments
/* ON subjects.id = departments.dep_id */
ON subjects.dep_id = departments.id
A "foreign key" in the subjects table for department is much more likely to be subjects.dep_id
Related
I am building a library management system using PHP, Mysql. There is a category for each book. Users will be able to see how many books there are in each category. But I can't figure out how many books there are in each category. I want to find out how many books there are in each category.
<div class="row">
<div class="col-md-12">
<!-- Advanced Tables -->
<div class="panel panel-default">
<div class="panel-heading">
Categories Listing
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>#</th>
<th>Category</th>
<th>Book Amount</th>
<th>Status</th>
<th>Creation Date</th>
<th>Updation Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT tblcategory.CategoryName, tblcategory.Status, tblcategory.CreationDate, tblcategory.UpdationDate from tblcategory";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<tr class="odd gradeX">
<td class="center"><?php echo htmlentities($cnt);?></td>
<td class="center"><?php echo htmlentities($result->CategoryName);?></td>
<td class="center">#</td>
<td class="center"><?php if($result->Status==1) {?>
Active
<?php } else {?>
Inactive
<?php } ?></td>
<td class="center"><?php echo htmlentities($result->CreationDate);?></td>
<td class="center"><?php echo htmlentities($result->UpdationDate);?></td>
<td class="center">
<a href="edit-category.php?catid=<?php echo htmlentities($result->id);?>"><button class="btn btn-primary"><i class="fa fa-edit "></i> Edit</button>
<a href="manage-categories.php?del=<?php echo htmlentities($result->id);?>" onclick="return confirm('Are you sure you want to delete?');"" > <button class="btn btn-danger"><i class="fa fa-pencil"></i> Delete</button>
</td>
</tr>
<?php $cnt=$cnt+1;}} ?>
</tbody>
</table>
</div>
</div>
</div>
<!--End Advanced Tables -->
</div>
</div>
My Database:
The Output Should be:
Calculate category wise book count by using subquery. Then use LEFT JOIN with main table. Use COALESCE() for replacing NULL value to 0.
SELECT c.CategoryName
, COALESCE(b.book_amount, 0) book_amount
, CASE WHEN c.Status = 1 THEN 'Active' ELSE 'Inactive' END Status
, c.CreationDate
, c.UpdationDate
FROM category c
LEFT JOIN (SELECT catid
, COUNT(id) book_amount
FROM book
GROUP BY catid) b
ON c.id = b.catid
ORDER BY b.book_amount DESC
I´m creating a school project and in this part I need to present the name of the person associated with this "n_processo" (id).
I have my tablets divided on the database like the printscreen, and I don´t know how to present the name of the person using the foreign key.
Turma Table:
I´m using a functions file, that have the function DBRead12 (that is selecting my "turma" table).
I have the user_especial table that have the names and the stranger key, like on this print.
User_Especial Table:
function DBRead12()
{
$sql="SELECT * FROM turma";
$result=DBExecute($sql);
while($res=mysqli_fetch_assoc($result))
{
$data[]=$res;
}
return $data;
}
<button class="collapsible">Consulta de Turmas</button>
<div class="content">
<br>
<div class="container" align="left">
<div class="well" align="left" style="width:70%">
<?php $admin = DBRead12()?>
<table id="example" class="table table-striped table-bordered" style="width:90%">
<thead>
<tr>
<th>Designação</th>
<th>Tipo</th>
<th>Diretor de Turma</th>
<th>Ano Letivo</th>
</tr>
</thead>
<tbody>
<?php foreach($admin as $cl)
{?>
<tr>
<td align=center><?php echo ($cl['designacao']) ?></td>
<td align=center><?php echo ($cl['tipo']) ?></td>
<td align=center><?php echo ($cl['diretor_turma']) ?></td>
<td align=center><?php echo ($cl['ano_letivo']) ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
In your query, you would use a join statement in order to get data from both tables at once
Example:
$sql="SELECT user_especial.nome, turma.*
FROM turma
LEFT JOIN user_especial ON user_especial.n_processo = turma.director_turma";
$sql="SELECT user_especial.nome as nome, turma.*
FROM turma
LEFT JOIN user_especial ON user_especial.n_processo = turma.director_turma";
This will give you all data from Turma Table and nome from User_Especial Table as nome.
$sql="SELECT user_especial.nome as nome, turma.*
FROM turma
LEFT JOIN user_especial ON user_especial.n_processo = turma.director_turma";
Also you must cut $cl['diretor_turma'] and add $cl['nome'].
So, your tbody will be:
<td align=center><?php echo ($cl['designacao']) ?></td>
<td align=center><?php echo ($cl['tipo']) ?></td>
<td align=center><?php echo ($cl['nome']) ?></td>
<td align=center><?php echo ($cl['ano_letivo']) ?>
</td>
I have a Laravel blade file where there is a table and i had loaded data using my web.php file. Now I want to add a table column for the vehicle brand. For that I need to access the brand table's data and I need to print the corresponding column brand in the table following are the codes.
This is my web.php
Route::get('/retailer/spares', function () {
$models = DB::table('models')->get();
$brands = DB::table('brands')->get();
$spares = DB::table('spares')->get();
return View::make('Retailer/spares')->with('models', $models)->with('brands', $brands)->with('spares', $spares);
});
This is my spares.blade.php please see the SQL query that had written. Other fields are working correctly.
<table class="table ">
<tr>
<th>Spare Id</th>
<th>Part Number</th>
<th>Name</th>
<th>Brand</th>
<th>Quantity</th>
<th>Price</th>
<th>Warranty</th>
<th>Spare Image</th>
<th>
<input type="text" class="form-control" id="search" placeholder="Search Spare">
</th>
</tr>
<tbody id="tableModel">
<?php
foreach($spares as $spare){
?>
<tr>
<td ><?php echo $spare->id;?></td>
<td ><?php echo $spare->partNumber;?></td>
<td ><?php echo $spare->description;?></td>
<td>
<?php
$brand="SELECT brandName FROM brands WHERE id=' $spare->brand_id'";
?>
</td>
<td ><?php echo $spare->quantity;?></td>
<td ><?php echo 'Rs.'. $spare->price;?></td>
<td ><?php echo $spare->warranty;?></td>
<td><div class="image"><?php echo ' <img class="img-responsive" src="data:image/jpeg;base64,'.base64_encode( $spare->image ).'"/>';?></div></td>
<td>
<a class=" btn btn-success btn-sm" data-toggle="modal" data-target="#modalEdit" onclick="EditBrand('<?php echo $brand->brandName;?>','<?php echo $brand->id;?>')" >Edit </a>
<a onclick="DeleteBrand(<?php echo $brand->id;?>)" style="" class=" btn btn-danger btn-sm" >Delete </a>
</td>
</tr>
<?php }?>
</tbody>
This is not good practice. You can join two table spares and brands in your controller. Try This.
$data = DB::table('brands')
->join('spares', 'brands.id', '=', 'spares.brand_id')
->select('brands.*', 'spares.id as spare_id', '',''....so on)
->get();
Here brands.* get all brands info you can manual get specific attribute from brands tabl as well as spares. You can get every info of brands and spares in $data array if Brands and spares has relationship
I am trying to group rows with similar ids (order_id in this case) together. I want each group of similar ids in a separate table. My presence query displays all rows in one table. I don't know how to go about it.
This is my code:
<div class='panel panel-body'>
<table class='table table-condensed'>
<thead>
<tr class='cart_menu'>
<td class='price'>Order ID</td>
<td class='price'>Item(s)</td>
<td class='price'>Quantity</td>
</tr>
</thead>
<tbody>
";
$select = "SELECT DISTINCT a.*, b.* FROM shipping_order a JOIN shipping_details b ON b.order_id = a.order_id WHERE user_id = :user_id AND a.order_id = b.order_id";
foreach ($db->query($select, array('user_id' => $id)) AS $items){
echo"
<tr>
<td class='cart_price'>
<h4>{$items['order_id']}</h4>
</td>
<td class='cart_price'>
<h4><a href=''>{$items['item']}</a></h4>
<!-- <p>Web ID: </p> -->
</td>
<td class='cart_price'>
<p>{$items['quantity']}</p>
</td>
</tr>
";
}
echo"
{$items['total']}
</tbody>
</table>
</div>
I have 3 table. purchase_order, goods_receive, purchase_transaction.
*** My purchase_order id is show as order_id.
1. I need sum all Quantity form goods_receive table as order id.
2. secondly I need sum amount form purchase_transaction tabel as order id.
see my attachment image for more clear >>
<?php
include("db.php");
$sqlnew="SELECT purchase_order.id , purchase_order.quantity,
purchase_order.unit_price, purchase_order.total_amount,
sum(goods_receive.quantity) as qua , sum(purchase_transaction.amount) as amount
FROM purchase_order
JOIN goods_receive ON purchase_order.id = goods_receive.order_id
JOIN purchase_transaction ON purchase_order.id = goods_receive.order_id
GROUP BY goods_receive.order_id";
$resultnew=mysql_query($sqlnew);
?>
<table class="table table-bordered data-table" border="1">
<thead>
<tr>
<th>Order ID</th>
<th>Quantity</th>
<th> Receive Quantity</th>
<th>Unit Price</th>
<th>Total Amount</th>
<th>Paid Amount</th>
</tr>
</thead>
<tbody>
<?php
while($rows=mysql_fetch_array($resultnew)){
?>
<tr class="gradeX">
<td><a href="purchase_order_single_details.php?id=<?php echo $rows['id']; ?>"><?php echo $rows['id']; ?></td>
<td><?php echo $rows['quantity']; ?></td>
<td><?php echo $rows['qua']; ?></td>
<td><?php echo $rows['unit_price']; ?></td>
<td><?php echo $rows['total_amount']; ?></td>
<td><?php echo $rows['amount']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<!--Footer-part-->
Please help me...
Join with subqueries that calculate the totals you want
SELECT po.order_id, po.quantity, gr.quantity AS receive_quantity,
po.unit_price, po.total_amount, pt.amount AS paid_amount
FROM purchase_order AS po
JOIN (SELECT order_id, SUM(quantity) AS quantity
FROM good_receive
GROUP BY order_id) AS gr ON gr.order_id = po.order_id
JOIN (SELECT order_id, SUM(amount) AS amount
FROM purchase_transaction
GROUP BY order_id) AS pt ON pt.order_id = po.order_id