I am currently fetching data from a group of location MySQL tables here is what I currently have
$query = "SELECT Name, CountryCode, Population FROM City WHERE (Population > '6000000') ORDER BY Population DESC";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "</tr>\n";
}
mysql_close($link);
The thing I am trying to figure out is how to convert the CountryCode to the actual country. Here is how I would call the country table to get the name
SELECT Name FROM Country WHERE Code = 'CountryCode';
How would I combine these two statements into one?
EDIT: Here is what I got to work. Is this considered a JOIN?
$query = "SELECT a.Name, a.CountryCode, a.Population, b.Code, b.Name FROM City a, Country b WHERE (a.CountryCode = b.Code) AND (a.Population > '6000000') ORDER BY Population DESC";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[4]</td>";
echo "</tr>\n";
}
mysql_close($link);
U need to join City table and Country table having one primary key and one foreign key.
Read about Natural Join. It's easy.
You can use this query:
$query = "SELECT city.Name,country.Name,city.CountryCode,city.Population from city INNER JOIN country where country.CountryCode=city.CountryCode and city.population>5000 ORDER BY city.Population DESC";
This is working fine.
Related
I want to return to the user a table that shows his orders but when a user orders one product the table is ok but in the moment that orders 2 product they get 2 lines with the same ID but different ProductID same total price.
$email = strval($_SESSION["email"]);
$TabellaOrdini = "SELECT DISTINCT ordini.IDOrdini, clienti.Indirizzo,
clienti.Nome, clienti.Cognome, ordini.StatoConsegna,
prodotti.Descrizione, ordini.TotaleOrdine,
pacco.IDProdotti, pacco.Quantita
FROM (((clienti NATURAL JOIN ordini)
JOIN pacco ON ordini.IDPacco = pacco.IDPacco)
JOIN prodotti ON pacco.IDProdotti = prodotti.IDProdotti)
WHERE clienti.IDClienti = ordini.IDClienti
AND clienti.Email = '$email'
ORDER BY ordini.IDOrdini";
$CheckOrdini = "SELECT insertphpordini.IDOrdini,
concat(insertphpordini.Descrizione,' x ',insertphpordini.Quantita) AS Risultato
FROM insertphpordini
WHERE insertphpordini.Email = '$email'
AND insertphpordini.IDOrdini IN
( SELECT insertphpordini.IDOrdini
FROM insertphpordini
GROUP BY insertphpordini.IDOrdini
HAVING COUNT(IDOrdini)>1
)";
$result = $conn -> query($TabellaOrdini);
$result2 = $conn -> query($CheckOrdini);
echo "<table id='customers'>";
echo "<tr><th colspan=7>Ordini</th></tr>";
echo "<tr>";
echo "<td><b>IDOrdine</b></td>";
echo "<td><b>Nome</b></td>";
echo "<td><b>Cognome</b></td>";
echo "<td><b>Indirizzo</b></td>";
echo "<td><b>Stato Consegna</b></td>";
echo "<td><b>Prodotto</b></td>";
echo "<td><b>Totale Ordine</b></td>";
while($row = $result -> fetch_assoc()) {
echo "<tr>";
echo "<td> $row[IDOrdini]</td>";
echo "<td> $row[Nome]</td>";
echo "<td> $row[Cognome] </td>";
echo "<td> $row[Indirizzo] </td>";
echo "<td> $row[StatoConsegna] </td>";
echo "<td> $row[Descrizione] / $row[Quantita]</td> ";
echo "<td> $row[TotaleOrdine]€ </td>";
echo "</tr>";
}
echo "</table>";
the query $TabellaOrdini get all values from a user whit that email and the query $CheckOrdini get only the values with the same ID and concat the Description and quantity as Risultato.
My goal is that is possible to have 1 order with 1 ID and in Product(row 6 from left of the table) have "ProductName1/Quantity1, ProductName2/Quantity2"
Use GROUP_CONCAT() to combine values from multiple rows into a single column.
$TabellaOrdini = "SELECT ordini.IDOrdini, clienti.Indirizzo, clienti.Nome, clienti.Cognome, ordini.StatoConsegna,
prodotti.Descrizione, ordini.TotaleOrdine, GROUP_CONCAT(pacco.IDProdotti, '/', pacco.Quantita) AS Prodotto
FROM clienti
NATURAL JOIN ordini
JOIN pacco ON ordini.IDPacco = pacco.IDPacco
JOIN prodotti ON pacco.IDProdotti = prodotti.IDProdotti
WHERE clienti.IDClienti = ordini.IDClienti AND clienti.Email = '$email' ORDER BY ordini.IDOrdini
GROUP BY ordini.IDOdini";
It's also not necessary to group your joins with ().
And you should stop substituting variables directly into SQL, and instead use prepared statements with bind_param().
You can use better display of Orders with Order ID, Customer details, Date in one row along with a drop down with all detailed products and quantities. This way you are not limited with tabular display option.
Else, you can use GROUP_CONCAT with ordini.IDOrdini (your OrderID) and display products as "product01, product02, product03" for same Order01 for instance.
Here some link for data-tables with child-rows :
https://datatables.net/examples/api/row_details.html
Or you can ajax it out for large child product sets :
https://datatables.net/blog/2017-03-31
NOTE: Please ensure PHP security and try to follow some design pattern for a clean code. Look for Database Object and Binding input parameters with $conn->prepare [https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php]
I have the following Query that I have to display in a HTML table:
$query= "SELECT SUM(amount_pln) AS suma,country FROM fin_revenues GROUP BY country ORDER BY suma DESC";
$result = mysqli_query($mysqli,$query);
When running the query in PHPMYADMIN, it works perfectly as I need but I can't figure out how to get the same result on a html table.
That's how is displayed in phpmyadmin:
Any help will be much appreciated.
$query= "SELECT SUM(amount_pln) AS suma,country FROM fin_revenues GROUP BY country ORDER BY suma DESC";
$result = mysqli_query($mysqli,$query);
echo "<table><tr><td>SUMA</td><td>country</td></tr>";
while($row = mysqli_fetch_assoc($result)){
echo "<tr><td>".$row['suma']."</td><td>".$row['country']."</td></tr>";
}
echo "</table>";
mysqli_free_result($result);
$query= "SELECT SUM(amount_pln) AS suma,country FROM fin_revenues GROUP BY country ORDER BY suma DESC";
$result = mysqli_query($mysqli,$query);
echo '<table>
<tr>
<td>Suma</td>
<td>Country></td>
</tr>';
while($row=mysqli_fetch_array($mysqli,$result)) // while there are records
{
echo "<tr><td>".$row['suma']."</td><td>".$row['country']."</td></tr>"; // echo the table with query results
}
echo '</table';
i use this table, and i was computing for the total of the SUM(quantity * price) but now i dont know how to use it for the specific serial from the customers table.
here is my query
<?php
$qry = "SELECT SUM(price * quantity), chever from order_detail";
$result = #mysql_query($qry);
while ($row=mysql_fetch_array($result)){
echo $row['chever'];
}
?>
i was using this but i didnt really understand it, i created the new query because SUM() messes up the old one, i dont kno why, but it shows 1 row only and no more.
$qry = "SELECT
order_detail.quantity,
order_detail.price,
customers.serial,
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.serial='{$_GET['serial']}'";
mysql_set_charset("UTF8");
$result = #mysql_query($qry);
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
echo "<div align='left'>";
echo "<table class='CSSTableGenerator' id=''>
<tr>
<td>Products</td>
<td>Quantity</td>
<td>Total Price</td>
<tr>";
while ($row=mysql_fetch_array($result)){
echo "<tr>";
//echo "<td>".$row['date']."</td>";
//echo "<td>".$row['name']."</td>";
//echo "<td>".$row['orderid']."</a></td>";
echo "<td>".$row['prod_name']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".($row['price']*$row['quantity'])."</td>";
//echo "<td>".$row['payment']."</td>"
echo "</tr>";
}
echo "</table>";
how do i join my tables and use the where clause.
This might do the trick
$qry = "SELECT SUM(order_detail.price * order_detail.quantity) AS totalOD, customers.serial from order_detail
LEFT JOIN orders ON orders.serial = order_detail.orderid
RIGHT JOIN customers ON customers.serial = orders.serial
WHERE
customers.serial='{$_GET['serial']}'";
$result = #mysql_query($qry);
while ($row=mysql_fetch_array($result)){
echo $row['totalOD'];
}
?>
I have two tables are subscribe and favorite.
subscribe [id subscribeid subscribeto datetime]
favorite [id wordid userid datetime]
So, I want to show these actively in news feed that can separate in each loop, where from which table?my code is look like this
$sql = "(SELECT * FROM favorite) UNION (SELECT * FROM subscribe) ORDER BY datetime DESC";
$result = mysql_query($sql)or die(mysql_error());
while($array = mysql_fetch_array($result)){
if($array['wordid']!=""){
echo "This's favorite feed";
}else{
echo "This's subscribed feed";
}
//echo $array['wordid'];
echo "<hr />";
}
In the condition of if($array['wordid']!="") I think it should work, but when in echo in commented line it always has a values, I don't know why is that.
Please help or suggest, thanks.
You can use alias function to union the result set. Based on cate field you can determine the results from which table.
Use mysqli extension instead of mysql
<?php
$sql = "SELECT id,
subscribeid As common_id,
subscribeto,
datetime, 'subscribe' As cate
FROM subscribe
UNION
SELECT id,
wordid As common_id,
userid As subscribeto,
datetime,
'favorite' As cate
FROM favorite";
$result = mysql_query($sql)or die(mysql_error());
while($array = mysql_fetch_array($result)){
if($array['cate']!="subscribe"){
echo "This's favorite feed";
}else{
echo "This's subscribed feed";
}
//echo $array['wordid'];
echo "<hr />";
}
I am looking for help. I have 3 tables which I have to query results from. In the table model, I have model_id and model, in the table colour, I have colour_id and colour and in the table availability, I have model_id and colour_id. I have to echo out the results in a table format. The table has to show the model, colour and the availability. In the availability section, if the model is available in a certain colour, it must show/return a yes value and if not show a no. Here is ma current code:
$sql = "SELECT model, colour FROM model, colour";
$query = "SELECT * FROM availability";
$result = mysql_query($sql);
$res =mysql_query($query);
echo "<table border='1'><trbgcolor='#cccccc'><td>Model</td><td>Colour</td><td>Availability</td></tr>";
while($row = mysql_fetch_array($result))
{
$model=$row['model'];
$colour=$row['colour'];
echo "<tr><td>$model</td><td>$colour</td>";
while($row = mysql_fetch_array($res))
{
$avail_id=$row['avail_id'];
$model_id=$row['model_id'];
$colour_id=$row['colour_id'];
}
if($res)
{
echo "<td>Yes</td>";
}
else
{
echo "<td>No</td></tr>";
}
}
echo "</table>";
SELECT m.model, c.colour, IF(c.colour_id IN (SELECT a.colour_id FROM availability a WHERE a.model_id = m.model_id), "YES", "NO") FROM model m, colour c ORDER BY m.model, c.colour
This should give you the results you want. Just format the output to your liking.