I just need a simple queries actually,
Here are my tables:
Information -> id_info, year, information_name, person_name, country_id, state_id, city_id
Country -> id, country_id, country_name
State -> id, state_id, country_id, state_name
City -> id, city_id, state_id, city_name
I have runs some queries, for example's:
SELECT *
FROM information, country, state, city
WHERE information.country_id =country.country_id
AND information.state_id = state.state_id
AND information.city_id = city.city_id
GROUP BY information.id_info;
My Simple Scripts:
echo '<td>'.$data['country_name'].'</td>
echo '<td>'.$data['state_name'].'</td>
echo '<td>'.$data['city_name'].'</td>
$data-> Is my while script with $query=mysql_query...
from the queries above only displaying two (2) data's from my database, but in the database it has five (5) data's.
Then I've trying to delete the Group statement, but the data kept looped and displaying almost 8000 data's, but I only got 5 data on tables.
I've tried everything, left join, right join, inner join....
I need help, I know it's quite simple, but how can I just display all the data normally.
Thanks.
Here My Full Scripts for displaying the data:
<table cellpadding="5" cellspacing="0" border="1">
<tr bgcolor="#CCCCCC">
<th>No.</th>
<th>Year</th>
<th>Information Name</th>
<th>Person Name</th>
<th>Country</th>
<th>State</th>
<th>City</th>
<th>Act</th>
</tr>
<?php
include('connect.php');
$query = mysql_query("SELECT *
FROM information
INNER JOIN country ON information.country_id =country.country_id
INNER JOIN state ON information.state_id = state.state_id
INNER JOIN city ON information.city_id = city.city_id
GROUP BY information.country_id, information.state_id, information.city_id") or die(mysql_error());
if(mysql_num_rows($query) == 0){
echo '<tr><td colspan="6">No data!</td></tr>';
}else{
$no = 1;
while($data = mysql_fetch_assoc($query)){
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$data['year'].'</td>';
echo '<td>'.$data['information_name'].'</td>';
echo '<td>'.$data['person_name'].'</td>';
echo '<td>'.$data['country_name'].'</td>';
echo '<td>'.$data['state_name'].'</td>';
echo '<td>'.$data['city_name'].'</td>';
echo '<td>Detail / Edit / Delete</td>';
echo '</tr>';
$no++;
}
}
?>
You can use INNER JOIN assuming that index key is properly used:
SELECT country.country_name,
state.state_name,
city.city_name
FROM information
INNER JOIN country ON information.country_id = country.country_id
INNER JOIN state ON information.state_id = state.state_id
INNER JOIN city ON information.city_id = city.city_id
Try changing your GROUP BY and use INNER JOIN :
SELECT *
FROM information
INNER JOIN country ON information.country_id =country.country_id
INNER JOIN state ON information.state_id = state.state_id
INNER JOIN city ON information.city_id = city.city_id
GROUP BY information.country_id, information.state_id, information.city_id
It worked now, *sigh the problem is the table in information, I have to empty the table first.
It quite a lot happened to me, the records was the problem source's, I've been through this a lot. Thank you guys for the help, now it's work perfectly fine.
The two answer above worked perfectly, Thank you.
Related
Hey guys i want to show a table so that table have 2 foreign keys.My food and restaurant table have name column.i tried following codes but i dont know how to show restaurant name and food name different.
$sql_select_food = "SELECT food.id,restaurant.name,food.name,food.restaurant_id,foodcategory.title from food INNER join foodcategory ON food.foodcategory_id=foodcategory.id INNER join restaurant ON food.restaurant_id=restaurant.id WHERE food.isActive = 1 ORDER BY food.updateDate DESC";
$result_select_food = mysqli_query($connection, $sql_select_food);
if (mysqli_num_rows($result_select_food) > 0) {
while ($row_select_food = mysqli_fetch_assoc($result_select_food)) {
echo "
<tr>
<td>$row_select_food[name]</td>
<td>$row_select_food[name]</td>
<td>$row_select_food[title]</td>
</tr>
";
}
}
Use column aliases:
SELECT food.id AS food_id, restaurant.name AS restaurant_name, food.name AS food_name ...
Then
$row_select_food['restaurant_name'] is the name of the restaurant,
$row_select_food['food_name'] is the name of the food,
etc.
Note: AS keyword is optional.
Put an alias to the column name and call it later in the code as a column name like :
$sql_select_food = "SELECT food.id,restaurant.name as rname, food.name as fname, food.restaurant_id,foodcategory.title from food INNER join foodcategory ON food.foodcategory_id=foodcategory.id INNER join restaurant ON food.restaurant_id=restaurant.id WHERE food.isActive = 1 ORDER BY food.updateDate DESC";
$result_select_food = mysqli_query($connection, $sql_select_food);
if (mysqli_num_rows($result_select_food) > 0) {
while ($row_select_food = mysqli_fetch_assoc($result_select_food)) {
echo "
<tr>
<td>$row_select_food[rname]</td>
<td>$row_select_food[fname]</td>
<td>$row_select_food[title]</td>
</tr>
";
}
}
I'm trying to create a fixture list for a football competition currently, and i have been stuck with trying to display something like "team1name V team2name".The tables that i am trying to pull the data from are:
Team
teamID
teamname
Fixtures
hometeam
awayteam
The sql query I have written up is:
$sql = "SELECT homeTeam, awayTeam, roundID, teamID, teamName, logo, groundName, abbreviatedName, matchDate, matchTime, venue
FROM fixtures
INNER JOIN team ON fixtures.homeTeam = team.teamID
INNER JOIN ground ON ground.groundID = team.groundID";
$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error());
and the PHP code which i have been using to display this data is:
<?php
while ($row = mysqli_fetch_array($results))
{
?>
<tr>
<td><?php echo $row ["teamName"]?></td>
<td> V </td>
<td><?php echo $row ["teamName"]></td>
</tr>
<?php
}
mysqli_close($conn);
?>
I understand i am calling teamname twice and hence why i am getting the same name displayed, but i am unsure as to how to get my code to differentiate the two IDs and names. Any help would be greatly appreciated
This is just a SQL problem. You need to join team twice, once for home team and once for away team.
SELECT homeTeamID, awayTeamID, homeTeam.teamID homeTeamID,
awayTeam.teamID awayTeamID, homeTeam.teamName homeTeamName,
awayTeam.teamName awayTeamName
FROM fixtures
INNER JOIN team homeTeam ON fixtures.homeTeam = homeTeam.teamID
INNER JOIN team awayTeam ON fixtures.awayTeam = awayTeam.teamID
INNER JOIN ground ON ground.groundID = team.groundID
By naming the selects, you can use the separately in your PHP code.
<?php echo $row['homeTeamName']; ?> v <?php echo $row['awayTeamName']; ?>
i have a question for the experts. I have no idea how to combine all the order ID with the same ID,
like in the picture, all orders with order ID = 1 are in one transaction, how do i combine all order IDs with the same id and display only one total price for all of the proudcts bought. and since all order ID are the same so the deliver and payment option will also be the same just need to display one row.
and a follow up question is how will i also show all the products bought if i combine all the same order IDs, compute the total price and display delivery and payment option in one single row
Here is the picture for my database relationship, i just followed the ones on the internet. the only difference is that "products" are changed to "inventory" and "serial" in products are changed to "prod_id"
here are my codes to display the current picture.
<?php
session_start();
$conn = #mysql_connect("localhost","root","");
$db = #mysql_select_db("");
$qry = "SELECT customers.name,customers.payment,customers.carrier, orders.date, order_detail.productid, order_detail.quantity, order_detail.price, order_detail.orderid, inventory.prod_name
FROM customers
RIGHT JOIN orders on customers.serial=orders.serial
RIGHT JOIN order_detail on orders.serial=order_detail.orderid
LEFT JOIN inventory on order_detail.productid=inventory.prod_id where customers.email='{$_SESSION['email']}'";
mysql_set_charset("UTF8");
$result = #mysql_query($qry);
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
echo "<center>";
echo "<table class='CSSTableGenerator'>
<tr>
<td>Date of Purchase</td>
<td>First Name</td>
<td>Order ID</td>
<td>Products</td>
<td>Quantity</td>
<td>Total Price</td>
<td>Delivery Option</td>
<td>Payment Option</td>
<tr>";
while ($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['orderid']."</td>";
echo "<td>".$row['prod_name']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".($row['price']*$row['quantity'])."</td>";
echo "<td>".$row['carrier']."</td>";
echo "<td>".$row['payment']."</td>";
echo "</tr>";
}
echo "</table>";
?>
</table>
It seems that what you're needing to learn for this particular question is the SQL Group By clause.
This (untested, might need minor tweaking) should do the trick. Note that you don't get order item quantities out of this, but I would say that it's possible to get them.
SELECT o.date, c.name, o.serial as 'Order Id', GROUP_CONCAT(p.name) as 'Products', SUM(od.price) as 'Total', c.carrier, c.payment
FROM orders o
JOIN customers c on o.customer_id = c.serial
JOIN orderdetail od on o.serial = od.orderid
JOIN products p on od.productid = products.prod_id
GROUP BY o.serial
Of particular note is a handy aggregate function in MySQL called GROUP_CONCAT.
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
I have 2 table of date , student_info and student_payment in my databace...
in student_info i have:
id, student_id,student_mail,student_pass,student_name,...
and in student_payment have:
id,student_id,student_payment_id,student_payment_date,...
so my problem is here, i wanna select student_name where student_id form student_info but i have problem and mysql give my an error:
$db->connect();
$sql = "SELECT * FROM `student_payment`";
$rows = $db->fetch_all_array($sql);
$student_id = $rows['student_id'];
$sql2 = "SELECT * FROM `student_info` WHERE student_id=$student_id";
$rows2 = $db->fetch_all_array($sql2);
$db->close();
foreach($rows as $record ){
// i wanna to use student_name in first line
echo "\n<tr>
<td>$record[student_id]</td>
<td dir=\"ltr\">$record[student_payment]</td>
<td dir=\"ltr\">$record[student_payment_id]</td>
<td dir=\"ltr\">$record[student_payment_bank]</td>
<td dir=\"ltr\">$record[student_payment_type]</td>
<td dir=\"ltr\">$record[student_payment_date]</td>
<td dir=\"ltr\"></td>
</tr>\n";
}
but i dont know how to connect student_id and student_name and use in foreach because i have 2 rows of data.
(i'm a beginner in PHP / MySql)
Instead of querying database twice, you can instead join the tables to get the rows you want. Try to execute the query below in PhpMyAdmin or directly on MySQL Browser.
SELECT a.*, b.*
FROM student_info a
INNER JOIN student_payment b
ON a.student_ID = b.student_ID
-- WHERE ...if you have extra conditions...
ORDER BY b.student_payment_date DESC
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
It is possible to fix it with INNER JOIN, you can join 2 tables and use both values from 1 query.
http://www.w3schools.com/sql/sql_join_inner.asp
Or you can use the OOP way, not sure if that is what you need.
Make 2 objects from the 2 query's and put them in a foreach.
try this
$sql2 = " SELECT * FROM `student_info` WHERE student_id= '$student_id' ";
try this
$sql2 = "SELECT * FROM `student_info` WHERE student_id IN ($student_id)";
foreach($rows as $record ){
// i wanna to use student_name in first line
echo "\n<tr>
<td>$record[student_id]</td>
<td dir=\"ltr\">".$record['student_payment']."</td>
<td dir=\"ltr\">".$record['student_payment_id']."</td>
<td dir=\"ltr\">".$record['student_payment_bank']."</td>
<td dir=\"ltr\">".$record['student_payment_type']."</td>
<td dir=\"ltr\">".$record['student_payment_date']."</td>
<td dir=\"ltr\"></td>
</tr>\n";
}
Use Mahmoud Gamal code
But always select the needed columns only not all because
In future number of columns in table may increase which may decrease the performance of your application.
Also it may contain some important information not to be leaked.
It seems like you want a report of all payments made. The student name is to be displayed with the payment information. The results will probably have more than one payment per student.
This result is ordered by student name, and then payment date (most recent first)
SELECT s.student_name, sp.*
FROM student_payment sp
INNER JOIN student_info s ON s.student_ID=sp.student_ID
ORDER BY s.student_name ASC, sp.student_payment_date DESC
try to join the table and use single query instead of two -
$sql = "SELECT * FROM student_info, student_payment
WHERE student_info.student_id=student_payment.student_id"
I am running a gameserver and for my gameserver I made a table that shows up your items.
The table cleared it up, but the problem is that there is a item called "GMP" is spamming the list because people got it verry much.
What I want to do is excluding the item "GMP" out of that whole table but I couldn't get it out of the table.
http://puu.sh/nefH . This is how it looks like.
Here is the part of the source code to let it show this.
print <<<END
<br>
<table border="1">
<tr>
<th>Item</th>
<th>+</th>
<th>Damage -</th>
<th>Extra hp</th>
<th>Position</th>
</tr>
END;
$sql6 = "SELECT t.name, i.position, p.position, i.type, i.magic3, i.reduce_dmg, i.add_life FROM cq_item i, position p, cq_itemtype t WHERE t.id = i.type AND p.id = i.position AND player_id = $id2 ORDER BY name ASC";
$execute6 = mysql_query($sql6);
while ($exibir6 = mysql_fetch_array($execute6)){;
print "<tr>";
print "<td>".$exibir6 ['name']."</td>";
print "<td>".$exibir6 ['magic3']."</td>";
print "<td>".$exibir6 ['reduce_dmg']."</td>";
print "<td>".$exibir6 ['add_life']."</td>";
print "<td>".$exibir6 ['position']."</td>";
print "</tr>";
}
print <<<END2
</table>
END2;
If there is something else needed just tell me and I will add it to here.
Sorry for my English it isn't my native language.
Don't select items with that name (use WHERE for that):
$sql6 = "
SELECT t.name, i.position, p.position, i.type, i.magic3, i.reduce_dmg, i.add_life
FROM cq_item i, position p, cq_itemtype t
WHERE t.id = i.type AND p.id = i.position AND player_id = $id2 AND t.name != 'GMP'
ORDER BY name ASC";
nb. I hope you escape/cast your query variables properly
$sql6 = "SELECT t.name, i.position, p.position, i.type, i.magic3, i.reduce_dmg, i.add_life FROM cq_item i, position p, cq_itemtype t WHERE t.id = i.type AND p.id = i.position AND player_id = $id2 AND t.name != 'GMP' ORDER BY name ASC";
Do an if statement in your loop.
<?php
while ($exibir6 = mysql_fetch_array($execute6)){
if($exibir6['name'] != 'GMP') {
// Add to table
}
}
?>