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
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 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
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 this table called bag:
+--------+----------+---------+----------+
| bag_id | chara_id | item_id | item_qty |
+--------+----------+---------+----------+
| 1 | 1 | 2 | 22 |
| 2 | 1 | 1 | 55 |
| 3 | 3 | 1 | 2 |
| 6 | 3 | 4 | 2 |
| 7 | 4 | 4 | 2 |
| 8 | 5 | 4 | 2 |
| 9 | 6 | 4 | 2 |
| 10 | 1 | 5 | 1 |
| 11 | 1 | 2 | 1 |
| 12 | 1 | 2 | 1 |
| 13 | 1 | 2 | 1 |
| 14 | 1 | 8 | 1 |
| 15 | 1 | 6 | 1 |
| 16 | 1 | 8 | 1 |
| 17 | 1 | 6 | 1 |
+--------+----------+---------+----------+
the relationship goes as 1 chara = many item
now i dont want 1 chara = many duplicated item.
how can i make a query that delete's the duplicated values?
like chara_id: 1 has 3 duplicated item_id: 2
i want to delete the other 2.
Not the best way to do it. But the below should definetly work:
Delete from Bag
where bag_id
not in (
select min(bag_id) from bag a,
(select chara_id, item_id
from bag group by chara_id, item_id
having count(*) > 1) b
where a.chara_id = b.chara_id and a.item_id = b.item_id
UNION
select bag_id from bag a,
(select chara_id, item_id
from bag group by chara_id, item_id
having count(*) = 1) b
where a.chara_id = b.chara_id and a.item_id = b.item_id
)
You can simply join table bag with a subquery which gets the minimum bag_id for every combination of chara_ID and item_ID. Records that have null values on any fields on the subquery are the records that will be deleted.
DELETE a
FROM bag a
LEFT JOIN
(
SELECT chara_ID, item_ID, MIN(bag_ID) min_ID
FROM bag
GROUP BY chara_ID, item_ID
) b ON a.bag_ID = b.min_ID AND
a.chara_ID = b.chara_ID AND
a.item_ID = b.item_ID
WHERE b.min_ID IS NULL
SQLFiddle Demo
Simple to say, but I can't find the syntax or even an example even close! Assume the following tables:
Table:'Red'
Fields: id | sm | md | lg
Data: 1 | 3 | 5 | 7
2 | 9 | 8 | 7
3 | 2 | 4 | 6
Table:'White'
Fields: id | sm | md | lg
Data: 1 | 0 | 0 | 0
2 | 0 | 0 | 0
3 | 0 | 0 | 0
Table:'Blue'
Fields: id | sm | md | lg
Data: 1 | 1 | 1 | 1
2 | 1 | 1 | 1
3 | 1 | 1 | 1
All i want is to total everything up, but keep the rows like the following table:
Table:'Total'
Fields: id | sm | md | lg
Data: 1 | 4 | 6 | 8
2 | 10 | 9 | 8
3 | 3 | 5 | 7
Then create a while loop in PHP to echo back the results. Something like this:
<?php
while($row = mysql_fetch_array($get_totals))
{
echo <td>".$row[sm]."</td><td>".$row[md]."</td><td>".$row[lg]."</td>";
}
?>
I can't figure this out. Any help? I just need a php select statement that will work here.
Not tested but should work:
SELECT id, SUM(sm) as sm, SUM(md) as md, SUM(lg) as lg FROM (
SELECT * FROM Red
UNION ALL
SELECT * FROM White
UNION ALL
SELECT * FROM Blue
) AS somealias
GROUP BY id