简体   繁体   中英

MySQL_Query from Multiple Tables with ROUND and Math

I'm trying to make a database of items for the game "EVE Online" for me and my friends to use, and I set up a SQL server and got tables created and the like, etc etc, and everything's running fine. However, I have one issue when I'm trying to import data from two different tables and compare them against each other within the same html table element. It works perfectly if I just import from one table with multiple columns, but I get issues when I try to select more than one source.

The tables I'm pulling from are...

testmetrics.eve_inv_types and testmetrics.items_selling

Ideally, I'd like to import the "name", "type_id", "jita_price_sell" columns from table #1, and "price", "type_id", "station_id", and "qty_avail" from table #2.

I'm also using the ROUND operator on jita_price_sell and price to get a 2 decimal approximation for price points of various items. I also have it so that in table #2 only results with the correct station_id will get displayed. But it keeps throwing up an error!

Here is my code so far...

<?php
$con = mysql_connect("testmetrics.db.10198246.xxxx.com","xxxx","xxxx");
if (!$con) 
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("testmetrics", $con);
$result = mysql_query("
SELECT testmetrics.eve_inv_types.name, testmetrics.eve_inv_types.type_id, ROUND(testmetrics.eve_inv_types.jita_price_sell, 2) as jita_price_sell, ROUND(testmetrics.items_selling.price, 2) as price, testmetrics.items_selling.qty_avail, testmetrics.items_selling.sation_id, testmetrics.items_selling.type_id 
FROM testmetrics.eve_inv_types, testmetrics.items_selling
WHERE testmetrics.eve_inv_types.type_id = testmetrics.items_selling.type_id, testmetrics.items_selling.station_id = '61000746'");
echo "<table class='sortable'>
<tr>
<th>Item Name</th>
<th>Price (Jita)</th>
<th>Price (K-6K16)</th>
<th>Qty Avail (K-6K16)</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['testmetrics.eve_inv_types.name'] . "</td>";
echo "<td>" . $row['jita_price_sell'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['testmetrics.items_selling.qty_avail'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>

Any help or insight you could give would be greatly appreciated. I'd also like to be able to do a math function where I'd go something like testmetrics.eve_inv_types.jita_price_sell + (testmetrics.eve_inv_types.volume * 300) to get shipping costs and have that exported to it's own column as well.

Anything you have to say is greatly appreciated!

EDIT: I know I'm already asking for alot of help here, but, does anyone know how to limit returns to "top 100" dependent on the column that it's sorted by? I'm using a Javascript addon to be able to sort easily!

This is the code that solved it for me!

SELECT eve_inv_types.name, 
       eve_inv_types.type_id, 
       Round(eve_inv_types.jita_price_sell, 2) AS jita_price_sell, 
       Round(items_selling.price, 2)           AS price, 
       items_selling.qty_avail,  
       items_selling.type_id 
FROM   eve_inv_types 
       JOIN items_selling
         ON eve_inv_types.type_id = items_selling.type_id 
            AND items_selling.station_id = '61000746' 
SELECT eit.name, 
       eit.type_id, 
       Round(eit.jita_price_sell, 2) AS jita_price_sell, 
       Round(eis.price, 2)           AS price, 
       eis.qty_avail, 
       eis.sation_id, 
       eis.type_id 
FROM   eve_inv_types eit 
       JOIN `items_selling` eis 
         ON eit.type_id = eis.type_id 
            AND eis.station_id = '61000746' 

You can join tables (assuming you have a key that connects them), here's an example:

table A:
    personId
    firstName
    lastName
    numOfChildren

table B:
    personId
    streetName
    cityName
    countryName

Select from both (using personID as a join key):

SELECT firstName, lastName, numOfChildren, streetName, cityName, countryName
FROM tableA JOIN tableB USING (personId)

If you want to limit the result you can add:

LIMIT 100

And if you want the top 100 parents you can order by numOfChildren :

ORDER BY numOfChildren

Final Query will look like this:

SELECT firstName, lastName, numOfChildren, streetName, cityName, countryName
FROM tableA JOIN tableB USING (personId)
ORDER BY numOfChildren
LIMIT 100;

-- or --

SELECT firstName, lastName, numOfChildren, streetName, cityName, countryName
FROM tableA JOIN tableB ON (tableA.personId=tableB.personId)
ORDER BY numOfChildren
LIMIT 100;

Read more about join here: http://dev.mysql.com/doc/refman/5.1/en/join.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM