$activeQuery = mysql_query("SELECT count(`status`) AS `active` FROM `assignments` WHERE `user` = $user_id AND `status` = 0");
$active = mysql_fetch_assoc($activeQuery);
$failedQuery = mysql_query("SELECT count(`status`) AS `failed` FROM `assignments` WHERE `user` = $user_id AND `status` = 1");
$failed = mysql_fetch_assoc($failedQuery);
$completedQuery = mysql_query("SELECT count(`status`) AS `completed` FROM `assignments` WHERE `user` = $user_id AND `status` = 2");
$completed = mysql_fetch_assoc($completedQuery);
There has to be a better way to do that, right? I don't know how much I need to elaborate as you can see what I'm trying to do, but is there any way to do all of that in one query? I need to be able to output the active, failed, and completed assignments, preferably in one query.
You can try something like this query
SELECT Status , COUNT(*) StatusCount
FROM assignments
WHERE Status IN (0, 1, 2)
AND User = $user_id
GROUP BY Status
Try this
$activeQuery = SELECT status, count(status) as "status count" FROM `assignments` WHERE `user` = $user_id GROUP BY `status`
edit: added group by
Instead of doing them individually you could use the following single SQL statement
SELECT count(*), `status`
FROM `assignments`
WHERE `user` = $user_id
AND `status` in (0,1,2)
GROUP BY `status`
ORDER BY `status`
The loop around the result set to extract the results.
SELECT
(SELECT COUNT(*) FROM `assignments` WHERE `user` = $user_id AND `status` = 0) AS active,
(SELECT COUNT(*) FROM `assignments` WHERE `user` = $user_id AND `status` = 1) AS failed,
(SELECT COUNT(*) FROM `assignments` WHERE `user` = $user_id AND `status` = 2) AS completed,
FROM `assignments`
GROUP BY active, failed, completed
Haven't checked the markup, but this is near or near enough.
Use the GROUP BY to get it in one query, but on separate rows.
$query = mysql_query("SELECT `status`, count(*) AS `num` FROM `assignments` WHERE `user` = $user_id AND `status` in (0,1,2) GROUP BY `status` ORDER BY `status` ASC");
$active_count = 0;
$failed_count = 0;
$completed_count = 0;
while ($array = mysql_fetch_assoc($query))
{
if ($array['status'] == 0)
$active_count = $array['num'];
else if ($array['status'] == 1)
$failed_count = $array['num'];
else if ($array['status'] == 2)
$completed_count = $array['num'];
}
When I have the option, I'm in the habit of using prepared statement(s) to help protect against sql-injection protection and for efficiency (requires using mysqli).
But if this is not an option, then
SELECT
CASE `status`
WHEN 0 THEN `active`
WHEN 1 THEN `failed`
WHEN 2 THEN `completed`
ELSE `unknown`
END
AS `statuslabel`,
COUNT(status)
AS `statuscount`
FROM `assignments`
WHERE `user` = $escaped_user_id
GROUP BY `statuslabel`
ORDER BY `statuslabel`
Note the use of the $escaped_user_id, which would be created beforehand:
$escaped_user_id = mysql_real_escape_string($user_id);
Related
I am trying to order in while from higger count of clicks to the lower and I am a bit of lost so I decided to ask the qustion here.
My code :
$stmt = $db->query("SELECT DISTINCT `country` FROM `entries` ORDER by `id` ASC");
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch()) {
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '{$row['country']}'")->rowCount();
$conversations = $db->query("SELECT `id` FROM `conversations` WHERE `country` LIKE '{$row['country']}' AND `link_id` = '{$id}'")->rowCount();
}
I want the foreach give me results from highest count of $clicks to the lowest.
Any ideas what can I do ?
You can do this query entirely in SQL with something like this (depending on SQL dialect):
$countries = $db->query("SELECT `country`, count(1) AS clicks FROM `entries` GROUP BY `country` ORDER BY clicks DESC");
foreach ($countries as $country) {
echo "${country['country']} has ${country['clicks']} clicks. ";
}
put number of clicks in an array as below :
$clicks[] = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '{$row['country']}'")->rowCount();
then you can simply sort the array by rsort php function as below :
echo rsort($clicks);
I have a really simple question, but unfortunately I can't figure it out myself. I have a list of 12 players which all have an (unique)ID, rating, attribute1 and attribute4. I ONLY want the row of the player with the highest rating followed by attribute1 and then attribute4. So it will first have to sort on rating, if there are 2 with rating 84, SQL will check for attribute1 etc. This is my code.
$sql = "SELECT * FROM `players_db` WHERE `id` = $player[0] OR `id` = $player[1] OR `id` = $player[2] OR `id` = $player[3] OR `id` = $player[4] OR `id` = $player[5] OR `id` = $player[6] OR `id` = $player[7] OR `id` = $player[8] OR `id` = $player[9] OR `id` = $player[10] OR `id` = $player[11] ORDER BY `rating` DESC , `attribute1` DESC, `attribute4` DESC";
$result = $conn->query($sql);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['rating'];
echo '<br>';
}
I can't figure it out how to go further, as I already tryed
SELECT MAX('rating') FROM players_db WHERE ...
But then it only gets the rating of the highest player, so how can I get the whole row of the player with the highest rating followed by attribute1 and attribute4?
I hope someone can help me out! Thanks!
This query will give the record corresponding to the player with the highest rating. In the event of a tie, attribute1 and attribute4 will be used to break the tie, in that order.
SELECT *
FROM players_db
ORDER BY rating DESC, attribute1 DESC, attribute4 DESC
LIMIT 1
Use LIMIT , So that first it will sort the records by rating, then it will select the first row from top AS used LIMIT 1:
$sql = "SELECT * FROM `players_db` WHERE `id` = $player[0] OR `id` = $player[1] OR `id` = $player[2] OR `id` = $player[3] OR `id` = $player[4] OR `id` = $player[5] OR `id` = $player[6] OR `id` = $player[7] OR `id` = $player[8] OR `id` = $player[9] OR `id` = $player[10] OR `id` = $player[11] ORDER BY `rating` DESC , `attribute1` DESC, `attribute4` DESC LIMIT 1";
$result = $conn->query($sql);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['rating'];
echo '<br>';
}
I am really noob in mysqli so i need help..
Please help me to convert this mysql to mysqli...i can't figure it out..
$new_mail = mysql_result(
mysql_query(
"SELECT COUNT(*) FROM `mail`
LEFT JOIN `contact` ON `mail`.`user_id` = `contact`.`from_id`
AND `contact`.`user_id` = '$user_id
WHERE `mail`.`from_id` = '$user_id'
AND `mail`.`read` = '0'
AND `mail`.`delete` != '$user_id'
AND `contact`.`ban` != '1'")
, 0);
if ($new_mail)
$list[] = 'Message - $new_mail '
I converted...tnx anyway :)
$new_mail = mysqli_fetch_array(mysqli_query($db, "SELECT COUNT(*) FROM `mail` LEFT JOIN `contact` ON `mail`.`user_id`=`contact`.`from_id` AND `contact`.`user_id`='$user_id' WHERE `mail`.`from_id`='$user_id' AND `mail`.`obrisano`!='$user_id' AND `contact`.`ban`!='1'"));
#ryan i just wanted to convert that mysql query to mysqli,but tnx :)
I was thinking of accomplishing the following as a PHP multi_query. But I'm trying to figure out how to pass the column value from the select query to the insert and update queries.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$query .= "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query .= "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
Problem is, I'm not sure how to pass the link_id value to the other queries. So I'm thinking I might have to rearrange the queries into one, but again, I'm just not sure how to pull that off.
Anyone got any suggestions?
You need to execute select query 1st then use its output to execute 2nd & 3rd query.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$query2 = "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query3 = "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
mysql_query($query2);
mysql_query($query3);
}
I'm trying to make a filter function for servers on my site. Users are able to check different options for the categories they want to filter for, and I have an ajax request that returns the servers satisfying their conditions. However, my mysql_query isn't working, I think I might have the wrong syntax.
By default, each category has the option set as 1. My current query is:
$order = "SELECT * FROM `servers`
WHERE `size` = '$size' or
1 = '$size' and
`type` = '$type' or
1 = '$type' and
`mode` = '$gamemode' or
1 = '$gamemode' and
`main` = '$main' or
1 = '$main'
ORDER BY $orderby
LIMIT 5";
It doesn't seem to get the correct servers, do I have an error in my query?
Thanks!
When mixing and and or in your query you must use parenthesis to group the conditions. Also, I think when you say 1 = '$size', I think you mean `size`=1.
$order = "SELECT * FROM `servers`
WHERE
(`size` = '$size' or `size` = '1') and
(`type` = '$type' or `type` = 1) and
(`mode` = '$gamemode' or `mode`= 1 ) and
(`main` = '$main' or `main` = 1)
ORDER BY $orderby
LIMIT 5";
"SELECT * FROM `servers` WHERE
`size` in ('$size', 1) and
`type` in( '$type' ,1) and
`mode` in('$gamemode' ,1) and
`main` in ( '$main' , 1 )
ORDER BY $orderby LIMIT 5";
You need to add parentheses because or and and have a different order of operations and needs parentheses to allow what you are needing to accomplish
SELECT * FROM `servers` WHERE
(`size` = '$size' or 1 = '$size') and
(`type` = '$type' or 1 = '$type') and
(`mode` = '$gamemode' or 1 = '$gamemode') and
(`main` = '$main' or 1 = '$main')
ORDER BY $orderby LIMIT 5
you must have to use brackets to use multiple OR condition in query
like
mysql_query("SELECT * FROM servers WHERE email='$Email' AND (date='$Date_Today' OR date='$Date_Yesterday' OR date='$Date_TwoDaysAgo' OR date='$Date_ThreeDaysAgo' OR date='$Date_FourDaysAgo')");
you can change with your column name
and also you can use IN condtion like
mysql_query("SELECT * FROM servers WHERE email='$Email' AND date IN ('$Date_Today','$Date_Yesterday','$Date_TwoDaysAgo')");
pls let me know if i can help you more
Just try to put brace around the or conditions
$order = "SELECT * FROM `servers` WHERE (`size` = '$size' or 1 = '$size') and (`type` = '$type' or 1 = '$type') and (`mode` = '$gamemode' or 1 = '$gamemode') and (`main` = '$main' or 1 = '$main') ORDER BY '$orderby' LIMIT 5";