Finding the min in a column where two other columns are zero - php

I am trying to write a MySQL search that will find the lowest imageID where the other two columns = 0. In this case the imageID returned would be 8.
ImageId Processing Finished
5 0 1
6 1 0
7 0 1
8 0 0
9 1 0
10 0 1
11 1 0
12 0 0
13 0 0
14 0 0
15 0 0

find the lowest imageID where the other two columns = 0
Just convert your sentence in the query:
(find) (the lowest imageID) (where the other two columns = 0)
[SELECT] [MIN(imageID)] [WHERE Processing = 0 AND Finished = 0]
So your full query should be (using MIN() aggregate function):
SELECT MIN(ImageId) as LowestImageId
FROM Mytable
WHERE Processing = 0
AND Finished = 0
See this SQLFiddle demo

select min(ImageId) from tablename where processing=0 and finished=0;
This is fairly basic SQL, and can easily be found if you do some research of your own.

SELECT MIN(ImageId) FROM your_table WHERE Processing = 0 AND Finished = 0

Related

MySQL using AND OR conditionals

I have a table with columns homeTeamId, and visitorTeamId.
I want to display the rows where homeTeamId = 39 or 43 AND visitorTeamId = 39 or 43
$sql ="SELECT * FROM game WHERE (homeTeamId='$_GET[homeTeamId]' or '$_GET[visitorTeamId]' and visitorTeamId='$_GET[homeTeamId]' or '$_GET[visitorTeamId]')";
gives me
homeTeamName visitorTeamName visitorTeamId homeTeamId visitorTeamScore homeTeamScore date
barrard teamName0 39 43 0 0 2016 01/17 09:41 pm
teamName0 barrard 43 39 0 0 2016 01/17 10:18 pm
teamName0 barrard 43 39 0 0 2016 01/17 10:26 pm
however, if i use something like 39 and 20 i get the exact same results.
i tried this way
$sql ="SELECT * FROM game WHERE homeTeamId=('$_GET[homeTeamId]' or '$_GET[visitorTeamId]') and visitorTeamId=('$_GET[homeTeamId]' or '$_GET[visitorTeamId]')";
and i get no result,
Thanks for your help.
Try this:
$sql ="SELECT * FROM game WHERE
(homeTeamId='$_GET[homeTeamId]' OR homeTeamId='$_GET[visitorTeamId]')
AND
(visitorTeamId='$_GET[homeTeamId]' OR visitorTeamId='$_GET[visitorTeamId]')";
You should try something like this:
$sql ="SELECT * FROM game WHERE (homeTeamId='$_GET[homeTeamId]' OR homeTeamId='$_GET[visitorTeamId]') AND (visitorTeamId='$_GET[homeTeamId]' OR visitorTeamId'$_GET[visitorTeamId]')";
Or use shorter aproach IN, posted as comment under your post!
Using IN, you can realize even more complex queries.
Using in:
$sql = "SELECT * FROM game WHERE homeTeamId IN ('$_GET[homeTeamId]',
'$_GET[visitorTeamId]') AND visitorTeamId IN ('$_GET[homeTeamId]',
'$_GET[visitorTeamId]')";
MySQL operators have an order of precedence. What that means is that some operators are evaluated before others.
We can use parenthesis to specify the order we want operators evaluated, to override the default order.
Consider:
SELECT '0 AND 1 OR 1' AS cond, 0 AND 1 OR 1
UNION ALL
SELECT '(0 AND 1) OR 1' AS cond, (0 AND 1) OR 1
UNION ALL
SELECT '0 AND (1 OR 1)' AS cond, 0 AND (1 OR 1)
returns:
cond 0 AND 1 OR 1
-------------- ------------
0 AND 1 OR 1 1
(0 AND 1) OR 1 1
0 AND (1 OR 1) 0

How to get row of the table with maximum value

I have a table as follow :
id date custid billno month amount balance
64 07-Jun-2015 1 102 5 9192 0
134 05-Jul-2015 1 172 6 9744 0
235 01-Aug-2015 1 277 7 4032 0
435 04-Sep-2015 1 461 8 3024 0
747 22-Sep-2015 1 597 9 2875 0
958 06-Nov-2015 1 789 10 3100 0
I want to get the row which has month max i.e. I want to get the row with month 10
If I use the following query then it gives the row with month 9
$get_lst = mysql_query("select * from `ledger` where `custid`='$custid' order by `month` DESC") or die(mysql_error());
$get_lst = mysql_fetch_assoc($get_lst);
print_r($get_lst);
Please help me what I am missing in the query ?
It looks like you are storing your values with the wrong type. The Date column should be type of DATE and your month column should be type of INT or even better TINYINT.
Storing month as VARCHAR causes the db to sort it as string. Ordering strings will start checking char by char (comparing '9' with '10' will result in '9' > '1').
After changing your column to either INT or TINYINT your query will work.
To increase the performance for larger tables, you could also use "LIMIT 1" in your query, in that way you won't fetch the whole table, if you only need one row.

Retrieve sum of two different table column field in mysql using php

Hey guys I m stuck over this query
I have two different table with same column name but different values with primary key id
Table 1 : q1
id ability_to_delegate communication confidence commitment
1 0 0 1 0
2 0 0 0 0
3 0 0 0 0
4 1 0 1 0
Table 2 : q2
id ability_to_delegate communication confidence commitment
1 0 0 2 1
2 0 0 1 1
3 0 0 0 0
4 0 0 1 1
Now what I want is to sum the values of two different tables with same field name but different IDs.
For example I want values of confidence field from table q1 with id = 4 i.e 1 and values of confidence field from table q2 with id = 1 i.e 2 to be added i.e 3.
I tried using union but not getting the rseult
$mresult=mysqli_query($con,"select sum(sm) from
(select confidence sm from q1 where id='$id'
union
select confidence sm from q2 where id='$id') ss");
while ($row1 = mysqli_fetch_assoc($mresult)){
echo "Sum ". $row1['ss'];
}
I m getting warning
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given in .... on line 89
Please help me out
The query to accomplish what you're looking for is
SELECT `q1`.`confidence` + `q2`.`confidence` AS `TotalConfidence`
FROM `q1`, `q2`
WHERE `q1`.`id` = 4
AND `q2`.`id` = 1
You can plug this into your PHP and substitute the variables where appropriate.
$mresult=mysqli_query($con,"SELECT `q1`.`confidence` + `q2`.`confidence` AS `TotalConfidence` FROM `q1`, `q2`WHERE `q1`.`id` = '{$q1id}' AND `q2`.`id` = '{$q2id}'");
while ($row1 = mysqli_fetch_assoc($mresult)){
echo "Sum ". $row1['TotalConfidence'];
}

Select max win series: sql query

MySQL
I have table, where i store user_matches and it result:
n_match id_user id_score
1 55 1
1 66 0
This mean, 'user with id=55 win match with id=1 to user with id=66'.
So, we have 10, 100, 1000 matches, where user win or lose to opponents:
n_match id_user id_score
1 55 1 (win)
1 66 0
2 55 0 (lose)
2 77 1
3 55 1 (win)
3 77 0
4 55 1 (win)
4 77 0
5 55 1 (win)
5 77 0
Ok. As u can see, user win 3 matches without losing (win series)- and that's what i need from my query.
Question: How could i get from this table the longest series of won matches? Is it possible without looping on sql side or server side- just from query?
Thx.
Edit: One of solution i just now understand,- to get all matches as string like 001010101111010101011, then split it into array of strings with separator '0' -> [1, 1, 1, 1111, ...] and just take the longest string length.
But in this case i have to write server side code =\ That's not good, but mb the fastest.
The best way to do this is to calculate the cumulative number of losses for any match. For a sequence of wins, this value is constant. You can then use group by to get the length of the longest such sequence.
This version of the query is database-neutral. It uses subqueries to get the counts:
select user_id, max(NumWinsInRow)
from (select user_id, cumlosses, count(*)-1 as NumWinsInRow
from (select m.*,
(select sum(case when id_score = 0 then 1 else 0 end) from user_matches m2 where m2.id_user = m.id_user and m2.n_match <= m.n_match
) as CumLosses
from user_matches m
) t
group by cumlosses, user_id
) t
group by user_id
This query should run faster if you have an index on user_matches(id_user, n_math, id_score).

How to rearrange the following data using php?

i retrieved the following data using sql query from mysql
TOTAL COMPUTER DATE GROUP
-----------------------------------------------
48 LAPTOP2 2009-08-19 1
77 LAPTOP2 2009-08-20 1
0 LAPTOP2 2009-08-21 1
15 LAPTOP1 2009-08-19 1
25 MAIN 2009-08-23 1
25 LAP3 2009-08-18 2
3 LAP3 2009-08-19 2
55 LAP3 2009-08-20 2
i would like to rearrange the data like using php
group computer 2009-08-18 2009-08-19 2009-08-20 2009-08-21 2009-08-22 2009-08-23
------------------------------------------------------------------------------------------------
1 LAPTOP2 0 48 77 0 0 0
1 LAPTOP1 0 15 0 0 0 0
1 MAIN 25
2 LAP3 25 3 55 0 0 0
Use the following query to pivot the data:
SELECT t.group,
t.computer,
MAX(CASE WHEN t.date = '2009-08-18' THEN t.total ELSE 0 END) AS '2009-08-18',
MAX(CASE WHEN t.date = '2009-08-19' THEN t.total ELSE 0 END) AS '2009-08-19',
MAX(CASE WHEN t.date = '2009-08-20' THEN t.total ELSE 0 END) AS '2009-08-20'
--, etc...
FROM TABLE t
GROUP BY t.group, t.computer
Your options are either to define each column for the data you are pivoting, or you can use MySQL's Prepared Statement syntax to dynamically create those columns.
I feel the need to point out that your example is inconsistent - for LAPTOP2, you have zero as the value for 2009-08-18, but the main value for that is blank. Neither have a record for that date. If you want these to show as blank/etc, change ELSE 0 END to ELSE NULL END in the CASE statements.

Categories