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>
";
}
}
Related
I have a table named "prices" in which I have two fields where I reference two other tables named "prodcuts" and "supliers". In the "prices" table I do reference to the "id" of the "prodcuts" and "supliers" and I need to get the name of both in a php code... What I have so far is this, but I don't know how to make it work:
$result = mysqli_query($conn, "SELECT * FROM prices") or die("Could not find");
if (mysqli_num_rows($result) > 0) {
while ($rr = mysqli_fetch_assoc($result)) {
$a = $rr['id'];
$b = $rr['date'];
$c = $rr['product'];
$d = $rr['suplier'];
$e = $rr['quantity'];
$f = $rr['packaging'];
$g = $rr['event'];
$h = $rr['price'];
$prov .="
<tr>
<td>".$b."</td>
<td><a href='productos-result.php?search=".$c."'>".$c."</a></td>
<td><a href='proveedores-result.php?search=".$d."'>".$d."</a></td>
<td>".$e."</td>
<td>".$g."</td>
<td>$".$h."</td>
</tr>";
}
}
I need to display the name of the product $c and suplier $d instead of the id, my tables are like this:
prices
id, date, productid, suplierid, quantity, event, price
product
id, name, description
suplier
id, name, description
Change your query to join the products and suppliers and have their names in the list of columns aliased to what you want.
SELECT prices.id,
prices.date,
product.name product,
suplier.name suplier,
prices.quantity,
prices.packaging,
prices.event,
prices.price
FROM prices
INNER JOIN product
ON product.id = prices.productid
INNER JOIN suplier
ON suplier.id = prices.suplierid;
(Change the INNER JOIN to LEFT JOIN if you have prices without supplier or product but want to show them anyway (with empty names).)
I have a search form where I can search for my webshop products.
1 product can be in multiple categoris, not just in one. I store this in the termek_katgoria_kapcsolo table. At insert, it creates as many lines, as the product belong to many categoria.
Example: The ID 12 product belong to ID 1, ID 2, ID 3 categoria.
The search sql only look at categoria, when one categoria is selected. Most often, I just search for the products name, I don't sort it to categoris.
How can I write the sql, that if I select a categoria also? I show you the tables on a pic.
if($termek_kategoria == 0 ) // Sort to categoria or not only search for product name, id...
{
$sql = "
SELECT termek_id, termek_nev, termek_cikkszam, termek_status FROM termek
WHERE $kereses_helye LIKE '%$kw%' ORDER BY $kereses_rendezes $kereses_sorrend
";
}
else
{
// Sorting for categoria also
$sql = "
SELECT termek_id, termek_nev, termek_cikkszam, termek_status FROM termek
WHERE $kereses_helye LIKE '%$kw%' AND termek_kategoria =
'$termek_kategoria' ORDER BY $kereses_rendezes $kereses_sorrend
";
}
Update:
$sql = "
SELECT termek.termek_id, termek.termek_nev, termek.termek_cikkszam, termek.termek_status
termek_kategoria_kapcsolo.*, termek_kategoria.kat_id
FROM termek
LEFT JOIN termek_katgoria_kapcsolo ON termek_kategoria
WHERE termek_kategoria_kapcsolo.kat_kapcs_kategoria_id = termek_kategoria.kat_id
AND termek.termek_id IN (SELECT kat_kapcs_termek_id FROM
termek_kategoria_kapcsolo WHERE kat_kapcs_kategoria_id = '$termek_kategoria')
";
This result:
Whats going wrong here?
What I want is when I select a categoria, the program give me the products, that are in the selected categoria.
I solved the problem:
$sql =
"
SELECT
t.termek_id,
t.termek_nev,
t.termek_cikkszam,
t.termek_status,
kapcs.kat_kapcs_kategoria_id,
kapcs.kat_kapcs_termek_id
FROM termek t
LEFT JOIN termek_katgoria_kapcsolo kapcs ON kapcs.kat_kapcs_kategoria_id = '$termek_kategoria'
WHERE t.termek_id = kapcs.kat_kapcs_termek_id AND t.$kereses_helye LIKE '%$kw%' ORDER BY t.$kereses_rendezes $kereses_sorrend
";
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.
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
}
}
?>
I have a script designed to print out values of students who have accrued more than 3 tardies. I want this script to print out both the student name, and the amount of times they've been tardy, but so far I've been only able to print out their names with the following script:
$sql = "select DISTINCT StudentID,classid,date from attendance_main where status = 'Tardy' AND classid like '%ADV%'";
$result = mysql_query($sql) or die (mysql_error());
while($row=mysql_fetch_array($result)) {
$studentid = $row['StudentID'];
$sql2 = "select distinct StudentID,classid,date from attendance_main where StudentID = '$studentid' AND status = 'Tardy' AND classid like '%ADV%'";
$result2 = mysql_query($sql2) or die (mysql_error());
while($row2=mysql_fetch_array($result2)) {
$tardycount = mysql_num_rows($result2);
$studentid = $row2['StudentID'];
if($tardycount >= 3) {
$sql3 = "select * from students where rfid = '$studentid'";
$result3 = mysql_query($sql3) or die (mysql_error());
while($row3=mysql_fetch_array($result3)) {
$fname[] = $row3['fname'];
}
}
}
}
$newdata = array_unique($fname);
foreach ($newdata as $value) {
echo $value;
}
I can't think of how to intuitively do this. Keeping it all in the while loop didn't work (I had multiple results coming up for the same students despite requesting unique entries) so using array_unique was the only method I could think of.
Thanks for any help!
Something like this:
SELECT
attendance_main.StudentID,
students.fname,
COUNT(attendance_main.*) AS `times_tardy`
FROM
attendance_main
INNER JOIN
students
ON
attendance_main.StudentID = students.rfid
WHERE
attendance_main.status = 'Tardy'
AND
attendance_main.classid like '%ADV%'
GROUP BY
attendance_main.StudentID
HAVING
`times_tardy` > 3
Joining the two tables gets you the tardy count and student's name in one query, and the GROUP BY and HAVING clause that get you only the students with more than 3 tardy entries.
You can (and should) do almost everything in SQL. It should look something like this.
select StudentID, classid, date count(*)
from attendance_main
where status = 'Tardy' AND classid like '%ADV%'"
left join student on student.rfid = attendance_main.StudentId
group by StudentId
having count(*) > 3;
Here's how it works.
select the results you want to work with:
select StudentID, classid, date count(*)
from attendance_main
where status = 'Tardy' AND classid like '%ADV%'"
Join the students to your result set on the common id
left join student on student.rfid = attendance_main.StudentId
group everything by student. We use count(*) to get the number of items. We know we're only dealing with tardies because of the where clause in the select.
group by StudentId
limit the results to only tardies above 3 with the having claues (this is like a where clause for the group by)
having count(*) > 3;