I have 2 tables (docs & doc_process):
docs
id | content | status
------------------------
1 | content 1 | 1
2 | content 2 | 2
doc_process
id | doc_id | user | status
---------------------------
1 | 1 | 1 | 2
2 | 1 | 2 | 2
3 | 1 | 3 | 2
4 | 2 | 1 | 1
5 | 2 | 2 | 2
6 | 2 | 3 | 1
How can I select the status when all doc_id = 1 have a value of 2
doc_id | status
---------------
1 | 2
doc_id 2 not filter because id = 4, id = 6 are not equal 2
Use a conditional aggregation
select doc_id
from your_table
group by doc_id
having sum(status <> 2) = 0
Related
I have a table as below:
CREATE TABLE IF NOT EXISTS `room_players`
(`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
,`player_id` int(11) NOT NULL
,`room_id` tinyint(1) NOT NULL
,`dealer` tinyint(1) NOT NULL
);
INSERT INTO room_players (`player_id`, `room_id`, `dealer`) VALUES
(1, 1, '0'),
(2, 1, '0'),
(3, 1, '0'),
(4, 1, '1'),
(5, 1, '0');
I need to get row after WHERE dealer='1' AND room_id='1'; in this case, it should return player_id = 5.
Now, when dealer = 1 for player_id = 5, then it should select the first player_id of the current room_id.
I tried:
SELECT *
FROM room_players
WHERE room_id='1'
AND id IN ( SELECT ID+1
FROM room_players
WHERE room_id='1'
AND dealer != 1
)
Selecting the next row works with this query but, when we got to the last player in line, it will return nothing.
I could run 2 different queries to get it work, but I would like to use an eloquent single query.
Using window functions we can achieve this in 1 query pretty easily:
first_value() asending id
first_value() descending id
lead()
case expression to figure out when to display next dealer value.
Just note: elegance of 1 query may make maintenance more complex.
it's not always better to build 1 sometimes two IS simplier.
Demo
you can adjust dealer to wherever you want and it will return the next dealer player number on the dealer number line. you didn't define expected results so I am unsure what you result is to look like.
SELECT *
, case WHEN dealer=1 and player_id = first_value(player_id) over (partition by room_id order by id desc)
THEN first_value(player_id) over (partition by room_id order by id)
WHEN dealer=1
THEN lead(player_id) over (partition by room_id order by id) end as NextDealer
From room_players
Giving us:
+----+-----------+---------+--------+------------+
| id | player_id | room_id | dealer | NextDealer |
+----+-----------+---------+--------+------------+
| 1 | 1 | 1 | 0 | |
| 2 | 2 | 1 | 1 | 3 |
| 3 | 3 | 1 | 0 | |
| 4 | 4 | 1 | 0 | |
| 5 | 5 | 1 | 0 | |
+----+-----------+---------+--------+------------+
or
+----+-----------+---------+--------+------------+
| id | player_id | room_id | dealer | NextDealer |
+----+-----------+---------+--------+------------+
| 1 | 1 | 1 | 0 | |
| 2 | 2 | 1 | 0 | |
| 3 | 3 | 1 | 0 | |
| 4 | 4 | 1 | 0 | |
| 5 | 5 | 1 | 1 | 1 |
+----+-----------+---------+--------+------------+
depending on who is dealer..
DB Fiddle expanded to show what's happening with case expression & show how to get dealer/room. Yes I had to use a CTE because the window functions can't have the data limited; it first must have the data materialized. But, if you create the CTE as a view and you're simply passing in the room to get the next dealer this works fine imo.
We can use:
WITH CTE AS (
SELECT *,
case WHEN dealer=1 and player_id = first_value(player_id) over (partition by room_id order by id desc)
THEN first_value(player_id) over (partition by room_id order by id)
WHEN dealer=1
THEN lead(player_id) over (partition by room_id order by id) end NextDealer
From room_players)
SELECT room_id, NextDealer FROM CTE WHERE NextDealer is not null
to get data by room and next dealer giving us: (I added some additional test data to make sure certain edge cases were working as expected)
+---------+------------+
| room_id | NextDealer |
+---------+------------+
| 1 | 7 |
| 2 | 99 |
+---------+------------+
With Date of:
+----+-----------+---------+--------+
| id | player_id | room_id | dealer |
+----+-----------+---------+--------+
| 1 | 1 | 1 | 0 |
| 2 | 2 | 1 | 0 |
| 3 | 3 | 1 | 0 |
| 4 | 4 | 1 | 1 |
| 5 | 7 | 1 | 0 |
| 6 | 99 | 2 | 0 |
| 7 | 14 | 2 | 0 |
| 8 | 22 | 2 | 0 |
| 9 | 77 | 2 | 1 |
| 10 | 15 | 2 | 0 |
+----+-----------+---------+--------+
I get:
+----+-----------+---------+--------+----+----+----+------------+
| id | player_id | room_id | dealer | a | b | c | NextDealer |
+----+-----------+---------+--------+----+----+----+------------+
| 1 | 1 | 1 | 0 | 7 | 1 | 2 | |
| 2 | 2 | 1 | 0 | 7 | 1 | 3 | |
| 3 | 3 | 1 | 0 | 7 | 1 | 4 | |
| 4 | 4 | 1 | 1 | 7 | 1 | 7 | 7 |
| 5 | 7 | 1 | 0 | 7 | 1 | | |
| 6 | 99 | 2 | 0 | 15 | 99 | 14 | |
| 7 | 14 | 2 | 0 | 15 | 99 | 22 | |
| 8 | 22 | 2 | 0 | 15 | 99 | 77 | |
| 9 | 77 | 2 | 1 | 15 | 99 | 15 | 15 |
| 10 | 15 | 2 | 0 | 15 | 99 | | |
+----+-----------+---------+--------+----+----+----+------------+
I went with slightly different approach, i got to solve it with one simple query, but did some coding with PHP also. Its not the eloquant i wanted, but it works for me.
function dealer($room_id) {
global $con;
$array = array();
$i = 0;
$cd = null;
$query = mysqli_query($con, "SELECT * FROM room_players WHERE room_id='".$room_id."'");
while($ft = mysqli_fetch_array($query)) {
$array[] = array("user_id" => $ft['user_id']);
if($ft['dealer'] == 1) {
$cd = $i;
}
$i++;
}
if(!is_null($cd)) {
if($array[$cd+1]) {
$next = $array[$cd+1];
}else{
$next = $array[0];
}
}else{
$next = $array[array_rand($array, 1)];
}
return $next['user_id'];
}
echo "Next dealer is: ". dealer(1);
I have a set of data stored in database table tb_Alt as follows :
id_alt |name|distance|price|quantity
1 |A | 2 | 10 | 3
2 |B | 4 | 123 | 4
3 |C | 1 | 201 | 1
4 |D | 5 | 145 | 10
And tb_Cri as follows :
id_criteria |name | weight |
1 |distance | 10
2 |price | 20
3 |quantity | 30
Now i want to insert data dynamically from tb_A into tb_B with structure as follows :
# |id_alt | id_criteria | value
1 | 1 | 1 | 2
2 | 1 | 2 | 10
3 | 1 | 3 | 3
4 | 2 | 1 | 4
5 | 2 | 2 | 123
6 | 2 | 3 | 4
7 | 3 | 1 | 1
8 | 3 | 2 | 201
9 | 3 | 3 | 1
10| 4 | 1 | 5
11| 4 | 2 | 145
12| 4 | 3 | 10
How can i achieve this through PHP?
Here's what i tried so far :
$data = $obj->selTable("SELECT * FROM tbb_Alt");
//$finishArr = transposeArr($data);
$totalLoop = count($data)*3;
$interval = (int)$totalLoop/3;
$counter = $totalLoop+$interval;
$j = 0;
$k = 0;
for($i=0;$i<=$counter;$i++){
if($i%4 !== 0){
if($k%4 == 0) $k=1;
// code to insert data into tb_B
// the part where i got stuck
echo $j.' '.$k++.' ';
continue;
}
$j++;
}
INSERT INTO tab_b (name,value)
SELECT name,value FROM (
SELECT id, 1 as id2, name,distance as value FROM tb_A
UNION SELECT id, 2 as id2, name,price as value FROM tb_A
UNION SELECT id, 3 as id3, name,quantity as value FROM tb_A
ORDER BY id, id2
) t;
It's always best to use in database calculations live example # SQL Fiddle
I have so problem to display different data in php mysql. I want to display differen data in two condition. Ex:
I have table :
tbl_hresponse
| id_reponse | id_atribut | skala | step |
| 1 | 1 | 1 | 1 |
| 1 | 2 | 4 | 1 |
| 1 | 3 | 2 | 1 |
| 1 | 1 | 5 | 2 |
| 1 | 2 | 6 | 2 |
| 1 | 3 | 2 | 2 |
so, I want to output like this :
| id_reponse | id_atribut | skala step 1 | skala step 2 |
| 1 | 1 | 1 | 5 |
| 1 | 2 | 4 | 6 |
| 1 | 3 | 2 | 2 |
i have code like this :
$k = $db->pdo->prepare("select *, (CASE WHEN tbl_hresponder.step = '1' THEN tbl_hresponder.skala END) AS k,
(CASE WHEN tbl_hresponder.step = '2' THEN tbl_hresponder.skala END) AS q
from tbl_hresponder, tbl_atribut
where tbl_hresponder.id_atribut = tbl_atribut.id_atribut
AND tbl_hresponder.id_responder = '".$_GET['report']."'
AND tbl_hresponder.id_fservqual = '".$rfs['id_fservqual']."'
AND tbl_hresponder.step = 1");
You can try to use CASE WHEN and max function.
SELECT t1.id_reponse,
t1.id_atribut,
max(case when t1.step = 1 then t1.skala end) `skala step 1`,
max(case when t1.step = 2 then t1.skala end) `skala step 2`
FROM tbl_hresponse t1
GROUP BY
t1.id_reponse,
t1.id_atribut
sqlfiddle:http://sqlfiddle.com/#!9/92a73e/4
Result
| id_reponse | id_atribut | skala step 1 | skala step 2 |
|------------|------------|--------------|--------------|
| 1 | 1 | 1 | 5 |
| 1 | 2 | 4 | 6 |
| 1 | 3 | 2 | 2 |
Hy I have 2 table
1.application
id | name | status
====================
1 | morvick | complete
2 | siti | prosess
3 | boby | complete`
2.application_test
id | application_id | test_id | result
======================================
1 | 1 | 1 | 70
2 | 1 | 2 | 80
3 | 1 | 3 | 90
4 | 2 | 1 | 60
5 | 2 | 2 | 80
6 | 2 | 3 | 70
7 | 3 | 1 | 90
8 | 3 | 2 | 70
9 | 3 | 3 | 60
10| 3 | 4 | 80
my Question is :
==================
1. how to find the maximum value at each test_id
2. how I can to get or total applicant_id where status complete
for example to be like this :
test_id | result_max | total_applicant_status(complete)
1 | 90 | 2
2 | 80 | 2
3 | 90 | 2
4 | 80 | 1
SELECT MAX(value) FROM table WHERE test_id = 1;
or perhaps SELECT value, test_id FROM table ORDER BY value DESC;
and for the next part, this may give what you want.
SELECT at.test_id, MAX(at.result), COUNT(IF(status='complete', 1, 0)) FROM application a LEFT JOIN application_test at ON a.id = at.application_id GROUP BY application_id;
I have a problem and don't find a way to solve it.
I have this mySQL Table:
product_insert
productInsertID | productID | .........
1 | 1
2 | 1
3 | 2
4 | 3
product_insert_options
productInsertID | optionsCategory | optionsValue
1 | 1 | Option1
1 | 2 | Option2
1 | 1 | More Option1
1 | 2 | More Option2
Now I have this php Variables
$productID = 1
$productOptionArray
[0][optionsCategory] = 1
[0][optionsValue] = More Option1
[1][optionsCategory] = 2
[1][optionsValue] = More Option2
Now I search the productInsertID for my php Variables. In this sample it's the ID 1. I can have more than 2 options, and one productInsertID can have more than one inserts.
How can I make it?
Sorry for my bad english.
EDIT (from answer):
It is a little bit difficult. I try with a sample shop cart and hope you now what I mean:
We have a shop with product and product attribute. Now we have this table: shop_cart
cartID | productID | userID | ...
1 | 1 | 1
2 | 2 | 1
3 | 1 | 2
4 | 1 | 1
Now we have the attribute. product_attribute
attributeID | productID | attribute
1 | 1 | Color
2 | 1 | Name
And we have the Table shop_cart_attribute
ID | cartID | attributeID | productID | value
1 | 1 | 1 | 1 | red
2 | 1 | 2 | 1 | Tina
3 | 3 | 1 | 1 | blue
4 | 3 | 2 | 1 | Tina
5 | 4 | 1 | 1 | blue
6 | 4 | 2 | 1 | Nina
Now i have this variable:
$productID = 1;
$userID = 1;
$productAttributeArray[0]['attributeID'] = 1;
$productAttributeArray[0]['value'] = red;
$productAttributeArray[1]['attributeID'] = 2;
$productAttributeArray[1]['value'] = Nina;
Now I search the cartID for my variables. In this sample its the CardID 4. The result must be empty or the cartID
I hope it's the better example.