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.
Related
Consider this table:
+-------------+--------------+-----------+------------+----------------+--------------------+
| Chapter(INT)| Unit (INT) | Item (INT)| WordNo(INT)| Word (VCHAR) |PartofSpeech(VCHAR) |
+-------------+--------------+-----------+------------+----------------+--------------------+
| 4 | 1 | 1 | 1 | He | pronoun |
| 4 | 1 | 1 | 2 | sells | verb |
| 4 | 1 | 1 | 3 | Apples | noun |
| 4 | 1 | 2 | 1 | Those | pronoun |
| 4 | 1 | 2 | 2 | apples | noun |
| 4 | 1 | 2 | 3 | are | verb |
| 4 | 1 | 2 | 4 | red | adjective |
| 4 | 1 | 3 | 1 | Green | adjective |
| 4 | 1 | 3 | 2 | grapes | noun |
| 4 | 1 | 3 | 3 | are | verb |
| 4 | 1 | 3 | 4 | delicious | adjective |
| 4 | 1 | 4 | 1 | These | pronoun |
| 4 | 1 | 4 | 2 | apples | noun |
| 4 | 1 | 4 | 3 | are | verb |
| 4 | 1 | 4 | 4 | rotten | adjective |
+------------------------------------------------------------------------------+
Now, say I want to be able to select each DISTINCT group of the 'Item' column WHERE Word = 'apple' AND PartofSpeech = 'adjective' and sum them. So based on this table, sum should be 2, and I should return the sentences for Item 2 ("Those apples are red.") and Item 4 ("These apples are rotten.").
I had assumed this query would work, but it seems I need to GROUP BY before I query, right?
"SELECT SUM(itemnum) AS totalitems
FROM (SELECT COUNT(DISTINCT Item) AS itemnum
FROM EnglishLevel1
WHERE Word = 'apples'
AND PartofSpeech = 'adjective'
GROUP BY Chapter, Unit, Item
) t"
I have also looked into the "IN" operator as well as changed AND to OR, but that doesn't get the desired result. Perhaps a subquery is required?
You may use aggregation as follows:
SELECT Chapter, Unit, Item,
GROUP_CONCAT(Word ORDER BY WordNo SEPARATOR ' ') AS sentence
FROM EnglishLevel1
GROUP BY Chapter, Unit, Item
HAVING SUM(Word = 'apple') > 0 AND SUM(PartofSpeech = 'adjective') > 0
ORDER BY Chapter, Unit, Item;
i have a Collection like that:
+----------+----------+
| user_id | city_id |
+----------+----------+
| 1 | 1 |
| 2 | 1 |
| ... | ... |
| 901 | 2 |
| 902 | 2 |
| ... | ... |
| 1501 | 3 |
| 1502 | 3 |
| ... | ... |
+----------+----------+
in this example i have
900 users for city_id = 1
600 users for city_id = 2
300 users for city_id = 3
what i want to do:
Sort each user in a fair way, so that I end up with a list like this:
+---------+----------+
| user_id | city_id |
+---------+----------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 901 | 2 |
| 902 | 2 |
| 1501 | 3 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
| 903 | 2 |
| 904 | 2 |
| 1502 | 3 |
+---------+----------+
I divided everything by 300 (for example), so i have by day:
3 users from city 1
then 2 users from city 2
then 1 user from city 3
then 3 users from city 1
then 2 users from city 2
then 1 user from city 3
...
I tried to use the take() function but it doesnt modify the original collection.
And i used the shift() function but it just take one item.
i tried that code:
while($users->isNotEmpty()){
// $user->count() = 1800
for ($i=0; $i<3; $i++){
$final->push($users->where('city_id', 1)->shift());
}
for ($i=0; $i<2; $i++){
$final->push($users->where('city_id', 2)->shift());
}
$final->push($users->where('city_id', 3)->shift());
// $user->count() = 1800; $final->count() = 6
}
it doesn't shift element from $user collection. still have 1800 elements...
Thanks !
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 |
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
I want to display dynamic mysql vertical data to horizontal in html table using PHP. And my table is like
mysql> select * from role_perm;
-------------------------------------------
| id | userID | roleID | permID | value |
--------------- ---------------------------
| 1 | 2 | 1 | 1 | 1 |
|------------------------------------------
| 2 | 2 | 1 | 2 | 0 |
|------------------------------------------
| 3 | 2 | 1 | 3 | 0 |
|------------------------------------------
| 4 | 2 | 2 | 4 | 0 |
-------------------------------------------
| 5 | 2 | 2 | 1 | 1 |
|------------------------------------------
| 6 | 2 | 2 | 2 | 1 |
|------------------------------------------
| 7 | 2 | 2 | 3 | 0 |
|------------------------------------------
| 8 | 2 | 2 | 4 | 1 |
-------------------------------------------
| 9 | 5 | 1 | 1 | 1 |
|------------------------------------------
| 10 | 5 | 1 | 2 | 0 |
|------------------------------------------
| 11 | 5 | 1 | 3 | 0 |
|------------------------------------------
| 12 | 5 | 1 | 4 | 0 |
-------------------------------------------
and so on...
and i want to display in html table like
----------------------------
| role | permissions |
----------------------------
| 1 | 1 | 2 | 3 | 4 |
----------------------------
| 2 | 1 | 2 | 3 | 4 |
----------------------------
| 3 | 1 | 2 | 3 | 4 |
-----------------------------
| 4 | 1 | 2 | 3 | 4 |
----------------------------
could you pls help me. Thank you in advance.
Try this:
SELECT
roleID AS role,
GROUP_CONCAT(DISTINCT permID ORDER BY permID ASC SEPARATOR '|') AS permissions
FROM role_perm
GROUP BY roleID
ORDER BY roleID
I suppose you could do this with a nifty MySQL query as well (which would supposedly be better for performance), but since I'm not that good at SQL, here's the PHP solution:
$arrRoles = array();
while ($row = mysql_fetch_assoc($result)) {
if (!isset($arrRoles[$row['roleID']])) {
$arrRoles[$row['roleID']] = array();
}
$arrRoles[$row['roleID']][$row['permID']] = $row['value'];
}
var_dump($arrRoles);