MySQL 18 queries required, or can I use just one? - php

I'm creating a golf score tracking database for some friends and am having trouble with the best way to retrieve a specific value from the result.
I have tables:
Players - stores player info
Hole - stores basic info about a hole (CourseID, HoleNum, Par...)
Score - stores each hole they played (HoleID, NumStrokes...)
My goal is to output a table showing their result for each hole (1-18). The following query gets me a list of all of the holes they played and the number of strokes it took them to complete the hole.
SELECT Hole.HoleID, NumStrokes
FROM Players
INNER JOIN Score ON Score.PlayerID = Players.PlayerID
INNER JOIN Hole ON Score.HoleID = Hole.HoleID
WHERE Players.PlayerID = '$PlayerID'
Results like this:
HoleID NumStrokes
1 4
1 6
1 3
2 5
7 3
Do I have to run 18 different queries, one for each hole that I am outputting, by adding:
AND Hole.HoleID = '$HoleID'
to my query, or is there a better SQL query that I could use? The Hole table also as a round identifier, so don't worry about determinig which NumStrokes of HoleID I need. I'm using PHP, by the way. Hopefully my question was clear enough.
Thank you!

$conn = new mysqli('host','user','pass','db_name');
$PlayerID = (int) $_GET['PlayerID'];
$rs = $conn->query("
SELECT Hole.HoleID, NumStrokes
FROM Players
INNER JOIN Score ON Score.PlayerID = Players.PlayerID
INNER JOIN Hole ON Score.HoleID = Hole.HoleID
WHERE Players.PlayerID = $PlayerID
") or die($conn->error);
while($row = $rs->fetch_object()) {
echo $row->HoleID.' - '.$row->NumStrokes.'<br/>';
}
This will output the results for a player. You can do the same with the mysql_* functions, but since they are deprecated its better to use mysqli or PDO

Related

Combine multiple queries to get single sum answer

Quite new to all the SQL/PHP stuff - dabbled with basic queries and outputting them to PHP previously but now trying something a bit more complicated and hoping someone can help with this as I've been trying to work it out with no luck so far:
I have 2 MS SQL tables:
Table 1 - Faults
faultid ... requestnumber
1 ........... 6
2 ........... 5
3 ........... 6
Table 2 - actions
faultid ....who ..... when...... timetaken
1.......... John....... Mon......... 1.00
2.......... Peter...... Mon.......... 2.00
3.......... Luke....... Tues........ 1.00
2.......... John....... Tues........ 0.5
1.......... Mike....... Mon......... 0.75
What I am trying to achieve is create a variable I can use in a front end php based webpage that gets a sum of the timetaken column in Table 2 where the requestnumber in Table 1 is equal to a specific number (i.e. 6)
I'm guessing it will start with something like:
$sql1 = "select faultid FROM Faults WHERE requestnumber = '6'";
$sqlresult = sqlsrv_query($conn, $sql1);
while ($row = sqlsrv_fetch_array($sqlresult)){
}
After that I get a bit stuck. How do I take each result from this and then run another query to get the sum of the timetaken column in Table 2 for just the corresponding faultid's? I want to hazard a guess at using foreach but not sure on the syntax (or even if I'm guessing correctly).
So in this example I would get back a result of 2.75 as a variable in PHP.
Any help would be appreciated. Thanks in advance.
Andy
Best to use just one SQL-statement
$sql = 'SELECT SUM(t2.timetaken)';
$sql .= ' FROM Faults t1 INNER JOIN actions t2 ON (t2.faultid = t1.faultid)';
$sql .= ' WHERE t1.requestnumber = ?';
Use this as prepared statement and pass your requestnumber (6 or something) as argument when executing this statement.
To get all or multiple sums you can use group by (maybe combine with WHERE):
SELECT t1.requestnumber, SUM(t2.timetaken)
FROM Faults t1
INNER JOIN actions t2 ON (t2.faultid = t1.faultid)
GROUP BY t1.requestnumber
Edit:
According to your own comment, you can use a SELECT with subquery, but use IN, not =. When using '=' the subquery must return only one row.
select SUM (timetaken) FROM actions WHERE faultid IN (select Faultid from Faults WHERE requestnumber = '6')
-- ^^
But this way is usually slower than the one I posted above

How to construct a more complex mysql query for optimization

I have this lovely piece of code that I want to optimize:
$result = mysql_query("select day from cities where name='St Charles'");
$row = mysql_fetch_assoc($result);
$day = $row['day'];
$result = mysql_query("select id,durability from goods_meta_data");
while($row = mysql_fetch_assoc($result)) {
mysql_query("delete from possessions where day_created<" . ($day-$row[durability]) . " and good_id=" . $row['id']);
}
It deletes rows from the table 'possessions' if the possession has expired. Whether or not the possession has expired is determined by values in the tables 'goods' and 'cities'.
Specifically, the age of the good = day-day_created, if the age of the good is more than its durability, then it is expired. (Every possession is a type of good, and every good has a unique durability.)
This code works, but I feel like this could be done in a single query.
The latency to the mysql server is particularly large, so doing this operation in less queries would be very beneficial.
How do I do that? Any ideas?
Also, if you can point me to any useful resources where I can learn about taking better advantage of relational databases in this manner, it would be helpful.
Assuming your first query returns a single result, then this should work:
DELETE p
FROM possessions p
INNER JOIN goods_meta_data gmd ON p.good_id = gmd.id
INNER JOIN cities c ON c.name = 'St Charles'
WHERE p.day_created < c.day - gmd.durability

Whats quicker - Multiple PDO queries or Array Query

I am just wondering which way to go with a database query. I have started using PDO recently with mysql. I am writing a small script that checks for manufacturers and then it checks items against each manufacturer. I am stuck whether it would be quicker to place the items in an array and (only use 1 query) then as i loop for the manufacturer array use an array_count_values to get the item quantities or do a seperate query in the loop to count the items.
I have about 400 manufacturers and 70000 items at present.
my current code using array is :
$itemquery = $conn->query("SELECT manufacturer FROM item_info_short");
$itemquery->execute();
$itemrow = $itemquery->fetchall(PDO::FETCH_ASSOC);
foreach ($itemrow as $itkey => $itvalue) {
$items[] = $itvalue[manufacturer];
}
$it_qty = array_count_values($items);
and then for my loop :
$manu_query = $conn->query("SELECT manufacturer FROM manufacturers ORDER BY manufacturer");
while($rowsx = $manu_query->fetch(PDO::FETCH_ASSOC)){
$rowid = $rowsx[manufacturer];
$count = $it_qty[$rowid];
if($count == '') $count = 0;
echo "<option value=\"$rowsx[manufacturer]\">$rowsx[manufacturer] $count Items</option>";
}
As you can see i use 2 PDO queries altogether.
The other method would use 401 queries.
I am trying to see which method is best practise and/or quicker.
Thanks in Advance for any advice.
Your question is irrelevant to PDO
You're doing it extremely inefficient way, but it's irrelevant to the question you've asked.
The question you have to ask have to be not "which is faster" but "which is proper way".
To get count of manufacturers with cout of their goods, you have to make SQL to count them for you
SELECT manufacturer, count(*) cnt FROM item_info_short GROP BY manufacturer
will return all manufacturers with their goods count
if you want to get manufacturer details along - join this query with manufacturers table
if you need to list all manufacturers with their goods - use LEFT JOIN
something like this
SELECT m.manufacturer, count(i.manufacturer) cnt
FROM manufacturers m LEFT JOIN item_info_short i
ON i.manufacturer = m.manufacturer GROUP BY m.manufacturer
Thanks 'Your Common Sense' for your assistance but it still did nt show me 0 results against manufacturers that were not in the 'item_info_short' table.
SELECT m.manufacturer,
(SELECT COUNT(i.manufacturer) FROM item_info_short i
WHERE m.manufacturer = i.manufacturer) cnt
FROM manufacturers m ORDER BY m.manufacturer ASC;
This was the final mysql statement I actually have used which gives me a full list of manufacturers and their quantities from item_info_short including 0 values. In answer to my own question this method is alot quicker than putting into an array first, and i believe this to be the correct way.

php sql multiple queries into one

Part of my page I have lots of small little queries, probably about 6 altogether, grabbing data from different tables. As an example:
$sql_result = mysql_query("SELECT * FROM votes WHERE voted_on='$p_id' AND vote=1", $db);
$votes_up = mysql_num_rows($sql_result);
$sql_result = mysql_query("SELECT * FROM votes WHERE voted_on='$p_id' AND vote=0", $db);
$votes_down = mysql_num_rows($sql_result);
$sql_result = mysql_query("SELECT * FROM kids WHERE (mother_id='$p_id' OR father_id='$p_id')", $db);
$kids = mysql_num_rows($sql_result);
Would it be better if these were all grabbed in one query to save trips to the database? One query is better than 6 isn't it?
Would it be some kind of JOIN or UNION?
Its not about number of queries but amount of useful datas you transfer. If you are running database on localhost, is better to let sql engine to solve queries instead computing results in additional programs. The same if you are thinking about who should be more bussy. Apache or mysql :)
Of course you can use some conditions:
SELECT catName,
SUM(IF(titles.langID=1, 1, 0)) AS english,
SUM(IF(titles.langID=2, 1, 0)) AS deutsch,
SUM(IF(titles.langID=3, 1, 0)) AS svensk,
SUM(IF(titles.langID=4, 1, 0)) AS norsk,
COUNT(*)
FROM titles, categories, languages
WHERE titles.catID = categories.catID
AND titles.langID = languages.
example used from MYSQL Bible :)
If you really want to lower the number of queries, you can put the first two together like this:
$sql_result = mysql_query("SELECT * FROM votes WHERE voted_on='$p_id'", $db);
while ($row = mysql_fetch_array($sql_result))
{
extract($row);
if ($vote=='0') ++$votes_up; else ++$votes_down;
}
The idea of joining tables is that these tables are expected to have something in between (a relation, for example).
Same is for the UNION SELECTS, which are prefered to be avoided.
If you want your solution to be clean and scalable in future, I suggest you to use mysqli, instead of mysql module of PHP.
Refer to: mysqli::multi_query. There is OOP variant, where you create mysqli object and call the function as method.
Then, your query should look like:
// I use ; as the default separator of queries, but it might be different in your case.
// The above could be set with sql statement: DELIMITER ;
$query = "
SELECT * FROM votes WHERE voted_on='$p_id' AND vote=1;
SELECT * FROM votes WHERE voted_on='$p_id' AND vote=0;
SELECT * FROM kids WHERE (mother_id='$p_id' OR father_id='$p_id');
";
$results = mysqli_multi_query($db, $query); // Returns an array of results
Fewer queries are (generally, not always) better, but it's also about keeping your code clear enough that others can understand the query. For example, in the code you provided, keep the first two together, and leave the last one separate.
$sql_result = mysql_query("SELECT vote, COUNT(*) AS vote_count
FROM votes
WHERE voted_on='$p_id'
GROUP BY vote", $db);
The above will return to you two rows, each containing the vote value (0 or 1) and the vote count for the value.

Enumerating tables used in mysql query?

Is there any way to enumerate tables used in mysql query?
Lets say I have query :
SELECT * FROM db_people.people_facts pf
INNER JOIN db_system.connections sm ON sm.source_id = pf.object_id
INNER JOIN db_people.people p ON sm.target_id = p.object_id
ORDER BY pf.object_id DESC
And I want in return array:
$tables = array(
[0] => 'db_people.people_facts',
[1] => 'db_system.connections',
[2] => 'db_people.people',
);
Yes, you can get information about tables and columns that are part of a query result. This is called result set metadata.
The only PHP solution for MySQL result set metadata is to use the MySQLi extension and the mysqli_stmt::result_metadata() function.
$stmt = $mysqli->prepare("SELECT * FROM db_people.people_facts pf
INNER JOIN db_system.connections sm ON sm.source_id = pf.object_id
INNER JOIN db_people.people p ON sm.target_id = p.object_id
ORDER BY pf.object_id DESC");
$meta = $stmt->result_metadata();
$field1 = $meta->fetch_field();
echo "Table for field " . $field1->name . " is " . $field1->table . "\n";
You'll have to build the array of distinct tables used in the query yourself, by looping over the fields.
Depending on what you're using it for, MySQL's EXPLAIN could do the trick for you:
http://dev.mysql.com/doc/refman/5.0/en/explain.html
The solution marked as good will return only the result tables. But if you do the next query it will fail:
SELECT users.* FROM users, cats, dogs WHERE users.id = cats.user_id
Will return only users and not cats and dogs tables.
The best solution is find a good parser, another solution is using REGEX and EXPLAIN query (more info in the next link):
Get mysql tables in a query
But I think that another good solution is list all tables and search them inside the query, you can cache the list of tables.
EDIT: When searching for tables, better use a preg like:
// (`|'|"| )table_name(\1|$)
if(preg_match('/(`|\'|"| )table_name(\1|$)/i', $query))
// found
If not, it can return false positives with for example "table_name2", "table_name3"... table_name will return FOUND two times.

Categories