MySQL using AND OR conditionals - php

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

Related

MySQL query on product attibute table

I have a mysql table which include product and attribute relations,
id productid attributeid arributevalueid
18 521 12 36
17 521 11 43
16 521 9 16
29 522 18 168
28 522 17 138
27 522 16 115
26 522 15 71
25 522 12 36
24 522 11 48
23 522 9 19
i got a problem when i write a sql query, i'm trying to to filter product which has all attributes,
ex - if i pass attribute value 16 and 36 the product matched is 521
i test a mysql query below
$nweryt=mysql_query('SELECT * FROM
tbl_proattrconnect
WHERE
tbl_proattrconnect.attroid=9 AND tbl_proattrconnect.attrvalid=16
AND tbl_proattrconnect.attroid=12 AND tbl_proattrconnect.attrvalid=36');
echo mysql_num_rows($nweryt);
But this cant get the results and show 0, can anyone help me please, thanks a lot
Please have a try with this:
SELECT
productid
FROM
tbl_proattrconnect
GROUP BY productid
HAVING SUM(attributevalueid IN (16, 36)) >= 2 /*here you specify after the >= how many attributes you have in IN()*/
AND SUM(attributevalueid BETWEEN x and y) >= 1 /*here you just always leave it at >= 1*/
AND SUM(attributevalueid BETWEEN z and w) >= 1 /*feel free to add as much checks as necessary.*/
This uses a little trick. attributevalueid IN (16, 36) returns true or false, 1 or 0.
Alternatively you can self join the table, but this may have a bit worse performance and can be quite clumsy if you have to check for more attributes. You have to self join the table once for each attribute.
see it working live in an sqlfiddle
You need to join the table to itself to make this happen. For example:
SELECT t1.productid
FROM tbl_proattrconnect t1
JOIN tbl_proattrconnect t2 ON t1.productid = t2.productid
WHERE
(t1.attroid = 9 AND t1.attrvalid = 16)
AND
(t2.attroid = 12 AND t2.attrvalid = 36)
You have a mistake in your query because you are using an AND operator and not OR.
Remember:
The AND operator displays a record if both the first condition AND the second condition are true.
The OR operator displays a record if either the first condition OR the second condition is true.
Read this info http://www.w3schools.com/sql/sql_and_or.asp
$nweryt=mysql_query('SELECT * FROM tbl_proattrconnect
WHERE
tbl_proattrconnect.attroid=9 AND tbl_proattrconnect.attrvalid=16
OR
tbl_proattrconnect.attroid=12 AND tbl_proattrconnect.attrvalid=36');
echo mysql_num_rows($nweryt);
Also can write the same query but shorter:
SELECT * FROM
tbl_proattrconnect
WHERE
tbl_proattrconnect.attroid IN(9,12) AND tbl_proattrconnect.attrvalid IN(16,36);
Maybe you meant to use OR rather than AND? Try again, this will display 521 and 522
$nweryt=mysql_query('SELECT * FROM
tbl_proattrconnect
WHERE
tbl_proattrconnect.attroid=9 AND tbl_proattrconnect.attrvalid=16
OR tbl_proattrconnect.attroid=12 AND tbl_proattrconnect.attrvalid=36
order by productid'
);

PHP/MySQL total by member

Like this a result :
75 Ansari 5 10
88 Koodoo 4 0
90 Koodoo 14 0
83 Koodoo 5 0
82 Koodoo, 6 0
81 Koodoo 4 0
79 Koodoo 5 0
74 Savage 1 0
80 Strike 2 36
87 Strike 4 15
78 Sullivan 3 15
77 Sullivan 2 0
I would like to get the total for each member for the last 2 columns (Hours and Minutes).
My query look like that :
SELECT
*
FROM
$tbl_name
ORDER BY
player
If someone would have a quick fix for that I would appreciate it.
Probably you want something like this (but you didn't provide a schema so I'm guessing on the field names).
SELECT `id`, `player`, SUM(`hours`) as hours, SUM(`minutes`) as minutes FROM `$tbl_name` GROUP_BY `player`;
You need to GROUP BY the user's name and SUM the totals for the hours and minutes columns.
A Simple example of this is:
SELECT `id`,
`user`,
SUM(`hours`) as tot_hours,
SUM(`mins`) as tot_mins
FROM `test`
GROUP BY `user`;
You can see this example run at this SQLFiddle
Note: With that fiddle, I removed the extra comma in this line (I assumed it was a typo)
82 Koodoo, 6 0
If that comma is supposed to be there, you just need to adjust that one insert statement to add the comma within the quotes of the name. That will adjust your query outcome as Koodoo, won't group with the other Koodoo values.

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).

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

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

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