Simple query for listing elements in mysql - mysql

I have two tables::
mysql> SELECT * FROM MasterListItemtype;
+----------------------+------------------------+
| MasterListItemtypeID | MasterListItemtypeName |
+----------------------+------------------------+
| 1 | Starters |
| 2 | Main Course |
| 3 | Side Course |
| 4 | Others |
| 5 | Desert |
+----------------------+------------------------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM MasterListMenuItems;
+------------------+-----------------------+-------+----------------------+
| MasterListMenuID | MasterListMenuName | Vegan | MasterListItemtypeID |
+------------------+-----------------------+-------+----------------------+
| 1 | Vada | 1 | 1 |
| 4 | Chinese Chicken Salad | 0 | 1 |
| 5 | Dosa Chicken Salad | 0 | 1 |
| 6 | Gobi Manchuri | 1 | 2 |
| 7 | Indian Salad | 1 | 3 |
| 8 | Gobi Chilli Manchuri | 1 | 2 |
| 9 | Chocolate IceCream | 1 | 5 |
| 10 | Vanilla IceCream | 1 | 5 |
+------------------+-----------------------+-------+----------------------+
8 rows in set (0.00 sec)
note here::
MasterListMenuID is primary key of MasterListMenuItems
MasterListItemtypeID is primary key of MasterListItemtype
MasterListItemtypeID is Foreign key of MasterListMenuItems
.
How to make a Mysql query so that::
I need to list the elements in MasterListMenuName of table MasterListMenuItems which are starters

try this:
select MasterListMenuName from MasterListMenuItems where MasterListItemtypeID
=
(
select MasterListItemtypeID from MasterListItemtype where MasterListItemtypeName
= 'Starters')
(OR)
select m.MasterListMenuName from MasterListMenuItems m
join MasterListItemtype n
on m.MasterListItemtypeID = n.MasterListItemtypeID
and m.MasterListMenuName = 'Starters'

SELECT * FROM MasterListMenuItems WHERE MasterListMenuItems.MasterListItemtypeID IN (SELECT MasterListItemtype.MasterListItemtypeID FROM MasterListItemtype WHERE MasterListItemtype. MasterListItemtypeName = 'Starters');

Try this
select MasterListMenuName from MasterListMenuItems mi
inner join MasterListItemtype it
on mi.MasterListItemtypeID = it.MasterListItemtypeID
where it.MasterListItemtypeName = 'Starters'

You can just use the foreign key for starter, in your case is 1.
SELECT * FROM MasterListMenuItems WHERE MasterListItemtypeID = 1

Related

How to get the overall count in a multiple selection system with ranking

I am designing a database for a web project in which I am trying to collect users' emotional feelings toward art collections. A hybrid dual list on the website allows users to select three options from 12 options and then rank the selected three as 1st, 2nd, and 3rd. There are 1000 data points in this project, which means there are 1000 distinct art collections for the users to vote on, each of the art collections has the same 12 options.
collection_poll
+-------+--------------------+
| id | collection_name |
+-------+--------------------+
| 1 | collection 1 |
| 2 | collection 2 |
| 3 | collection 3 |
| 4 | collection 4 |
| 5 | collection 5 |
| ... | ... |
+-------+--------------------+
option
+--------------------+--------------------+----------------+
| collection_id | option id | Text |
+--------------------+--------------------+----------------+
| 1 | 1 | Emotion 1 |
| 1 | 2 | Emotion 2 |
| 1 | 3 | Emotion 3 |
| 1 | 4 | Emotion 4 |
| 1 | 5 | Emotion 5 |
| 1 | 6 | Emotion 6 |
| 1 | 7 | Emotion 7 |
| 1 | 8 | Emotion 8 |
| 1 | 9 | Emotion 9 |
| 1 | 10 | Emotion 10 |
| 1 | 11 | Emotion 11 |
| 1 | 12 | Emotion 12 |
| 2 | 1 | Emotion 1 |
| 2 | 2 | Emotion 2 |
| 2 | 3 | Emotion 3 |
| 2 | 4 | Emotion 4 |
| 2 | 5 | Emotion 5 |
| ... | ... | ... |
+--------------------+--------------------+----------------|
vote
+--+-------+-------------+-------------+-------------+-------------+
|id|user_id|collection_id|1st_option_id|2nd_option_id|3rd_option_id|
+--+-------+-------------+-------------+-------------+-------------+
|1 | 1 | 1 | 1 | 8 | 12 |
|2 | 2 | 1 | 3 | 1 | 8 |
|3 | 3 | 1 | 1 | 8 | 3 |
|4 | 1 | 2 | 1 | 8 | 12 |
|5 | 2 | 2 | 3 | 12 | 8 |
|6 | 3 | 2 | 1 | 3 | 12 |
+--+-------+-------------+-------------+-------------+-------------+
This table records each vote and specifies which collection the user votes and the 1st, 2nd, and 3rd options the user ranks.
How do I use MySQL to get this table?
+---------------+-----------+-----------+-----------+-----------+
| collection_id | option_id | 1st_count | 2nd_count | 3rd_count |
+---------------+-----------+-----------+-----------+-----------+
| 1 | 1 | 2 | 1 | 0 |
| 1 | 2 | 0 | 0 | 0 |
| 1 | 3 | 1 | 0 | 1 |
| 1 | 4 | 0 | 0 | 0 |
| ... ... ... ... ... |
| 1 | 8 | 0 | 2 | 1 |
| ... ... ... ... ... |
| 1 | 12 | 0 | 0 | 1 |
| 2 | 1 | 2 | 0 | 0 |
| 2 | 2 | 0 | 0 | 0 |
| 2 | 3 | 1 | 1 | 0 |
| ... ... ... ... ... |
| 2 | 8 | 0 | 1 | 1 |
| ... ... ... ... ... |
| 2 | 12 | 0 | 1 | 2 |
| ... ... ... ... ... |
+---------------+-----------+-----------+-----------+-----------+
which uses collection_poll.id and option.id to get the result from table_vote.
I have
CREATE
OR REPLACE VIEW "public"."poll_results_first_option_count" AS
SELECT
vote.collection_id,
vote.first_option_id,
count(*) AS first_count
FROM
vote
GROUP BY
vote.collection_id,
vote.first_option_id
ORDER BY
vote.collection_id,
vote.first_option_id;
to get the count of each rank but cannot put them together.
The accepted answer does not provide the correct results. You need to be LEFT JOINing, starting from your option table and including option_id in the JOIN criteria:
SELECT `o`.`collection_id`, `o`.`option_id`,
`1st_count`,
`2nd_count`,
`3rd_count`
FROM `option` `o`
LEFT JOIN ( SELECT `collection_id`, `1st_option_id` AS `option_id`, COUNT(*) AS `1st_count` FROM `vote` GROUP BY 1, 2 ) AS `t1` USING (`collection_id`, `option_id`)
LEFT JOIN ( SELECT `collection_id`, `2nd_option_id` AS `option_id`, COUNT(*) AS `2nd_count` FROM `vote` GROUP BY 1, 2 ) AS `t2` USING (`collection_id`, `option_id`)
LEFT JOIN ( SELECT `collection_id`, `3rd_option_id` AS `option_id`, COUNT(*) AS `3rd_count` FROM `vote` GROUP BY 1, 2 ) AS `t3` USING (`collection_id`, `option_id`)
ORDER BY `o`.`collection_id`, `o`.`option_id`;
In your question you state:
each of the art collections has the same 12 options
If this is the case, then there is no need to link them in your option table. Instead you can just have the twelve options (and drop collection_id column) and CROSS JOIN to collection_poll to build the full list:
SELECT `cp`.`id`, `o`.`option_id`,
`1st_count`,
`2nd_count`,
`3rd_count`
FROM `collection_poll` `cp`
CROSS JOIN `option` `o`
LEFT JOIN ( SELECT `collection_id`, `1st_option_id` AS `option_id`, COUNT(*) AS `1st_count` FROM `vote` GROUP BY 1, 2 ) AS `t1` ON `cp`.`id` = `t1`.`collection_id` AND `o`.`option_id` = `t1`.`option_id`
LEFT JOIN ( SELECT `collection_id`, `2nd_option_id` AS `option_id`, COUNT(*) AS `2nd_count` FROM `vote` GROUP BY 1, 2 ) AS `t2` ON `cp`.`id` = `t2`.`collection_id` AND `o`.`option_id` = `t2`.`option_id`
LEFT JOIN ( SELECT `collection_id`, `3rd_option_id` AS `option_id`, COUNT(*) AS `3rd_count` FROM `vote` GROUP BY 1, 2 ) AS `t3` ON `cp`.`id` = `t3`.`collection_id` AND `o`.`option_id` = `t3`.`option_id`
ORDER BY cp.id, o.option_id;
Step 1: Do the sums
SELECT collection_id,
1st_option_id,
COUNT(*) AS 1st_count
FROM votes
GROUP BY 1,2
and do similarly for the others
Step 2: Put them together (somewhat like "pivot"):
SELECT collection_id,
1st_count,
2nd_count,
3rd_count
FROM ( ... ) AS t1
JOIN ( ... ) AS t2 USING(collection_id)
JOIN ( ... ) AS t3 USING(collection_id)
ORDER BY collection_id;
where the "..." comes from step 1

Curious as to know why this doesn't work MySQL

What I'm trying to do is get every teacher name involved with the class_id provided. And it does nothing, very strange.The tables the query is connected to:
+----+------------+----------+
| id | teacher_id | class_id |
+----+------------+----------+
| 1 | 1 | 2 |
| 6 | 1 | 4 |
| 10 | 2 | 3 |
| 5 | 2 | 5 |
| 4 | 3 | 1 |
| 7 | 3 | 3 |
| 9 | 4 | 2 |
| 2 | 4 | 4 |
| 3 | 5 | 1 |
| 8 | 5 | 5 |
+----+------------+----------+
10 rows in set (0,00 sec)
mysql> select * from classes;
+----------+------------+------------------------+
| id_class | class_name | responsible_teacher_id |
+----------+------------+------------------------+
| 1 | Geography | 2 |
| 2 | Reading | 1 |
| 3 | Science | 3 |
| 4 | History | 5 |
| 5 | Arithmetic | 4 |
+----------+------------+------------------------+
5 rows in set (0,00 sec)
mysql> select * from teachers;
+----+------------+-------------+-----------+------------+------------+-------------+----------------------------------+
| id | first_name | middle_name | last_name | birthdate | subject_id | current_age | md5 |
+----+------------+-------------+-----------+------------+------------+-------------+----------------------------------+
| 1 | Oliver | Weed | Saxon | 1983-01-03 | 2 | 38 | 4bf51ce48af83dba7c8390a7222db991 |
| 2 | Dexter | Tristan | Morgan | 1980-12-11 | 5 | 40 | be96d6213828fd09538d8b1357907d54 |
| 3 | Deborah | Ocean | Morgan | 1985-06-10 | 2 | 35 | ba6869ece0a74ca9987b83b4f840405b |
| 4 | George | Kai | Smith | 1965-01-03 | 6 | 56 | ccec54863dbb6b63e55d495341fd9a63 |
| 5 | Chi | Kai | Chian | 1979-10-03 | 3 | 41 | 2291ce528bf05eff905af1eecd9bc53b |
+----+------------+-------------+-----------+------------+------------+-------------+----------------------------------+
5 rows in set (0,00 sec)
And the query:
select first_name
, middle_name
, last_name
FROM teachers_seth t
JOIN classes_sethputz c
ON c.responsible_teacher_id = t.id
JOIN teachers_classes_sethputz t_c
ON t_c.class_id = c.id_class
AND t_c.teacher_id = t.id
WHERE c.id_class = 1;
And what the query returned each time I ran it with a different class_id:
c.id_class = 1;
Empty set (0,00 sec)
c.id_class = 2;
+------------+-------------+-----------+
| first_name | middle_name | last_name |
+------------+-------------+-----------+
| Oliver | Weed | Saxon |
+------------+-------------+-----------+
1 row in set (0,00 sec)
c.id_class = 3;
+------------+-------------+-----------+
| first_name | middle_name | last_name |
+------------+-------------+-----------+
| Deborah | Ocean | Morgan |
+------------+-------------+-----------+
1 row in set (0,00 sec)
c.id_class = 4;
Empty set (0,00 sec)
c.id_class = '5';
Empty set (0,00 sec)
Thanks. This is really strange for me.
You have too much joining going on for your question, you have 3 tables, teachers, classes and a matrix that links them many-many.
You are joining the classes table directly to the teachers table with the responsible_teacher_id column, this should not be there if you want the teachers involved with the classes. What you are doing is saying if the teachers are the responsible teacher AND the teacher is involved in the class and that is rarely the case so you get very few hits.
So to solve it, start with the class table (classes_sethputz), as that is what you appear to be doing and then add in the matrix table (teachers_classes_sethputz) by joining it then add in the teachers table (teachers_seth) by joining it to the matrix table (teachers_classes_sethputz).
SELECT first_name
, middle_name
, last_name
FROM classes_sethputz c
JOIN teachers_classes_sethputz t_c
ON t_c.class_id = c.id_class
JOIN teachers_seth t
ON t.id = t_c.teacher_id
WHERE c.id_class = 5;

concat and group concat from a table that join with another table

How to use concat and group concat from a table that join with another table.
The schema looked like this :
FIRST TABLE :
MariaDB [ittresnamuda]> select * from tb_tipe_request;
+---------+------------+
| id_tipe | nama_tipe |
+---------+------------+
| 1 | Perbaikan |
| 2 | Permintaan |
+---------+------------+
2 rows in set (0.00 sec)
SECOND TABLE
MariaDB [ittresnamuda]> select a.ID_REQUEST, a.CATATAN from tb_requestfix a;
+------------+---------------------------------+
| ID_REQUEST | CATATAN |
+------------+---------------------------------+
| 3 | Akan kami cek jaringan tersebut |
| 4 | Iya, go ahead. Appproved |
| 5 | Sudah di refill |
| 28 | Saja |
+------------+---------------------------------+
4 rows in set (0.00 sec)
THIRD TABLE
MariaDB [ittresnamuda]> select * from tb_link_tipe_request;
+----+------------+---------+
| id | id_request | id_tipe |
+----+------------+---------+
| 8 | 4 | 1 |
| 9 | 4 | 2 |
| 11 | 3 | 1 |
| 12 | 5 | 2 |
| 40 | 28 | 1 |
+----+------------+---------+
5 rows in set (0.00 sec)
I already use join, concat, and group_concat, but still no result. I need to select the table like this :
+------------+---------------------------------+------------------------+
| ID_REQUEST | CATATAN | TIPE_REQUEST |
+------------+---------------------------------+------------------------+
| 3 | Akan kami cek jaringan tersebut | Perbaikan |
| 4 | Iya, go ahead. Appproved | Perbaikan / Permintaan |
| 5 | Sudah di refill | Permintaan |
| 28 | Saja | Perbaikan |
+------------+---------------------------------+------------------------+
For the help, thanks a lot.
You can join all the tables together, and then use GROUP_CONCAT like this:
select a.ID_REQUEST, a.CATATAN ,group_concat(t.nama_tipe separator ',') as tipe_request
from tb_requestfix a
INNER JOIN tb_link_tipe_request at
ON(a.id_request = at.id_request)
INNER JOIN tb_tipe_request t
ON(t.id_tipe = at.id_tipe)
GROUP BY a.id_request

Having difficulty with the WHERE IN clause for MySQL

Ok, I realize this may be incredibly simple, but my brain is frozen right now. Need a bit of assistance with this query. Let's break it down.
I have two tables (per this example) and I want to update a single table "undeliverable" status
Customers Table (tbl_customers):
+------------+-------------+
| customerID | custAcctNum |
+------------+-------------+
| 1 | 100100121 |
| 2 | 100100122 |
| 3 | 100100123 |
| 4 | 100100124 |
| 5 | 100100125 |
+------------+-------------+
Address Table (tbl_address):
+-----------+------------+---------------+
| addressID | customerID | undeliverable |
+-----------+------------+---------------+
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 3 | 0 |
| 4 | 4 | 0 |
| 5 | 5 | 0 |
+-----------+------------+---------------+
Dataset with "undeliverable" Customer Account numbers (custAcctNum)
100100121, 100100123, 100100124
And the query will update the Address Table to this
+-----------+------------+---------------+
| addressID | customerID | undeliverable |
+-----------+------------+---------------+
| 1 | 1 | 1 |
| 2 | 2 | 0 |
| 3 | 3 | 1 |
| 4 | 4 | 1 |
| 5 | 5 | 0 |
+-----------+------------+---------------+
This is the query that I have tried to use
UPDATE tbl_address
SET undeliverable = 1 WHERE
( SELECT custAcctNum FROM tbl_customers AS c
INNER JOIN tbl_address AS a ON a.customerID = c.customerID )
IN ( 100100121, 100100123, 100100124);
Any suggestions? Thanks!
Use mysql's multiple-table update syntax:
update tbl_Address t
join custAcctNum c
on c.customerid = t.customerid
set t.undeliverable = 1
where c.custAcctNum in (100100121, 100100123, 100100124)
UPDATE tbl_address
SET (undeliverable = 1)
WHERE customerID IN (
SELECT customerID
FROM tbl_customers
WHERE custAcctNum IN (100100121, 100100123, 100100124)
);

Update data in Multiple table

Three table as follow
mysql> select * from food;
+--------+------+-------+
| foodid | name | price |
+--------+------+-------+
| 1 | 雞 | 100 |
| 2 | 鴨 | 200 |
| 3 | 魚 | 300 |
| 4 | 肉 | 400 |
+--------+------+-------+
4 rows in set
mysql> select * from drink;
+---------+------+-------+
| drinkid | name | price |
+---------+------+-------+
| 1 | 紅茶 | 50 |
| 2 | 綠茶 | 100 |
| 3 | 奶茶 | 150 |
+---------+------+-------+
3 rows in set
mysql> select * from order_table;
+----+-----------+--------+---------+------------+-------------+-------------+
| id | user_name | foodid | drinkid | food_count | drink_count | total_price |
+----+-----------+--------+---------+------------+-------------+-------------+
| 2 | 小明 | 3 | 2 | 2 | 2 | 0 |
| 3 | 小華 | 1 | 1 | 1 | 8 | 0 |
| 4 | 小英 | 1 | 3 | 3 | 3 | 0 |
| 6 | 小a | 2 | 1 | 4 | 6 | 0 |
| 7 | 小b | 2 | 2 | 5 | 4 | 0 |
| 8 | 小c | 2 | 3 | 6 | 10 | 0 |
| 9 | 大A | 3 | 1 | 9 | 8 | 0 |
| 10 | 大B | 3 | 2 | 5 | 4 | 0 |
| 11 | 大C | 3 | 3 | 10 | 3 | 0 |
+----+-----------+--------+---------+------------+-------------+-------------+
foodid in order_table is link to foodid in food table,
drinkid in order_table is link to drinkid in drink table,
Now, I want to calculate total price,
Total_price =
order_table.foodid(food.price in food table) * order_table.food_count +
order_table.drinkid(drink.price in drink table) * order_table.drink_count;
So, let me knowlege the command to update total price
thx a lot.
The reason why I used LEFT JOIN on the following query is because I assumed that some orders may only contain drinks or foods.
UPDATE order_table a
LEFT JOIN food b
ON a.foodid = b.foodID
LEFT JOIN drink c
ON a.drinkID = c.drinkID
SET a.total_price = (a.food_count * COALESCE(b.price, 0) +
a.drink_count * COALESCE(c.price, 0))
SQLFiddle Demo
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
Something like this should be close:
SELECT COALESCE(F.Price,0)*OT.Food_Count+COALESCE(D.Price,0)*OT.Drink_Count Total_price
FROM Order_Table OT
LEFT JOIN Food F ON OT.FoodId = F.FoodId
LEFT JOIN Drink D ON OT.DrinkId = D.DrinkId
And to actually update that column:
UPDATE Order_Table OT
LEFT JOIN Food F ON OT.FoodId = F.FoodId
LEFT JOIN Drink D ON OT.DrinkId = D.DrinkId
SET OT.Total_Price = COALESCE(F.Price,0)*OT.Food_Count+COALESCE(D.Price,0)*OT.Drink_Count