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'];
}
Related
i have two tables. 'listing_messages' and 'listing_message_history'.
listing_messages
=============
message_id listing_id listing_user third_party_user
1 162 7 32
2 162 7 33
3 162 7 12
listing_message_history
======================
listing_message_history message_id is_checked
1 1 0
2 1 1
3 1 1
4 2 0
5 2 1
6 3 0
Search criteria==> listing_user=7, is_checked=1
I want result..
message_id count_of_unread_message_history
12
2 1
3 0
I have made a query is
SELECT count(`lmh`.`message_history_id`) AS COUNT,
`lmh`.`message_id`
FROM `listing_message_history` AS `lmh`
LEFT JOIN `listing_messages` AS `lm` ON `lmh`.`message_id` = `lm`.`message_id`
WHERE `lm`.`third_party_user`=7
AND `lmh`.is_checked=1
GROUP BY `lm`.`message_id`
but it does not returns message_id with count = 0 which message_id do not have is_checked=1
In general, when you use left join, filter conditions on the second table should go in the on clause not the where clause. Try this:
SELECT count(`lmh`.`message_history_id`) AS COUNT,
`lmh`.`message_id`
FROM `listing_message_history` `lmh` LEFT JOIN
`listing_messages` `lm`
ON `lmh`.`message_id` = `lm`.`message_id` AND
`lm`.`third_party_user` = 7
WHERE `lmh`.is_checked = 1
GROUP BY `lmh`.`message_id`;
In addition, you should use the first table for the group by, not the second table.
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).
What I want to do is to select a value of the database,
Lets say:
id ---- giftid ---- userid
1 1 481
2 1 422
3 7 123
4 9 542
5 1 122
6 1 455
For example, there are 4 users that want to have the same giftid:
1, 2, 5, 6
It means that each one will have 25% to be chosen.
How can I make the "percent selection"?
Assuming every userid can only claim a giftid once, you can use the ORDER BY RAND() in MySQL. This will firstly select all the rows from table table where the giftid is 1 and then the results are ordered randomly. The LIMIT 1 ensures that only the first record is returned
SELECT * FROM table
WHERE giftid = `1`
ORDER BY RAND()
LIMIT 1
Are you looking this?
SELECT giftid, 1.0 / COUNT(*) percentSelection
FROM tableName
GROUP BY giftid
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
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.