I have a table where it contains user details. Now I need to execute a MySQL query which satisfies the below conditions:
There are two tables
1) user table - which contains userid,name,firstname
2) category table - which containts categoryid, category type, assinged userid
Now I want to execute the query which satisfies the below condition
A User can only sees a category with a type of simple,complex and which are assigned only to his userid and that category shouldn't get assigned to any another userid.
Anyone please help me in this.
drop table if exists category;
create table category (id int, categorytype varchar(10), assignedUserId int);
truncate table category;
insert into category values
(1,'simple',1),
(2,'complex',1),
(3,'simple',2),
(4,'complex',3),
(5,'odd',1);
MariaDB [sandbox]> select * from users where id < 6;
+----+----------+----------+--------+---------------------+
| id | userName | photo | status | ts |
+----+----------+----------+--------+---------------------+
| 1 | John | john.png | 1 | 2016-12-08 13:14:24 |
| 2 | Jane | jane.png | 1 | 2016-12-08 13:14:24 |
| 3 | Ali | | 1 | 2016-12-08 13:14:24 |
+----+----------+----------+--------+---------------------+
3 rows in set (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> select u.*, c.*
-> from users u
-> join category c on c.assigneduserid = u.id
-> where u.id = 1
-> and c.categorytype not in (select c.categorytype from category c where c.assigneduserid <> 1);
+----+----------+----------+--------+---------------------+------+--------------+----------------+
| id | userName | photo | status | ts | id | categorytype | assignedUserId |
+----+----------+----------+--------+---------------------+------+--------------+----------------+
| 1 | John | john.png | 1 | 2016-12-08 13:14:24 | 5 | odd | 1 |
+----+----------+----------+--------+---------------------+------+--------------+----------------+
1 row in set (0.00 sec)
Try this:
SELECT *
FROM category c
where userid = 123
and not exists (
select 1
from category c2
where c.userid <> c2.userid
and c.categorytype = c2.categorytype
);
Note that this query doesn't require the id to be mentioned at two places unlike the other answer, and hence is a more general one.
SELECT * FROM categories
WHERE category_type = 'simple' OR
category_type = 'complex' AND // (edited after comment of Philipp)
assinged_userid = (Your User ID Here)
Related
I'm building a survey system where
A client that has many users
A user can have multiple surveys.
Client A might have users [1,2,3].
Now there's a table user_completions, which shows which users have completed my survey.
users table
+----+-----------+
| id | client_id |
+----+-----------+
| 1 | 1 |
| 2 | 1 |
+----+-----------+
user_completions table
+---------+-----------+
| user_id | survey_id |
+---------+-----------+
| 1 | 1 |
| 2 | 2 |
+---------+-----------+
How can I (in one query) see how many users have a record in the user_completion table, grouped by survey, and only for the current client
The desired result would look something like
+-----------+-------+-----------------------+
| survey_id | users | num_persons_completed |
+-----------+-------+-----------------------+
| 1 | 2 | 1 |
| 2 | 2 | 1 |
+-----------+-------+-----------------------+
I think you're missing couple of table mentioning in your question but if you really have only these 2 tables than you can achieve this with following query:
select tmp.survey_id, count(distinct tmp.user_id) completed,
(select count(distinct id) from users where client_id = 1) users
from
(select t1.*, t2.*
from users t1
join user_completions t2
on t1.id = t2.user_id
where t1.client_id = 1
)tmp
group by tmp.survey_id
I have 2 tables
table A
tag_id | Tag_name
1 | tg1
2 | tg2
3 | tg3
4 | tg4
table B
id | name |tag_id
1 | avq | 1,2,4
2 | bdq | 2
3 | abc | 3,2
4 | vdf | 1,4
5 | zxc | 3
I want to inner join both tables and get its count using tag_id in the following format
`tg1=> 2,tg2=> 3,tg3=> 2,tg4=> 2`
How is it possible in a single MySQL query?
The best option is to normalize the 2nd table and create an association table for storing the tag id and the id of the 2nd table. In the meanwhile the following should do the job but for long run you need to normalize the table else more problems will happen in future
select
t1.Tag_name, count(*) as total
from tableA t1
join tableB t2 on find_in_set(t1.tag_id,t2.tag_id) > 0
group by t1.tag_id ;
You need to create relation table. For example:
Tag table:
+----+----------+
| id | name |
+----+----------+
| 1 | Tag name |
+----+----------+
| 2 | Tag 2 |
+----+----------+
B Table:
+----+-----------+
| id | title |
+----+-----------+
| 1 | Any title |
+----+-----------+
Reference table ex. :
+------+--------+
| b_id | tag_id |
+------+--------+
| 1 | 1 |
+------+--------+
| 1 | 2 |
+------+--------+
In your reference table you put many tags for one B element. In this example you see two tags assigned by reference to b_id = 1
select tag_name, count(position)
from (
select a.tag_name, FIND_IN_SET(a.tag_id,b.tag_id) as position
from a,b
) as tmpTB
where position !=0
group by tag_name
I would like to know how to select from one table and count from another (loop) using a single query
Table ctagories
------------------------------
cat_id | cat_name | parent_id
------------------------------
1 | General | 0
------------------------------
2 | News | 0
------------------------------
3 | Sports | 1
------------------------------
4 | Test | 0
------------------------------
Table posts
--------------------------------------
post_id| title | c_id | active
--------------------------------------
1 | test | 1 | 1
--------------------------------------
1 | test 1 | 2 | 0
--------------------------------------
1 | test 2 | 1 | 1
--------------------------------------
1 | Test 3 | 3 | 1
--------------------------------------
I want display categories where parent_id=0 (main categories) with the post count (posts where active = 1) in front of it
Ex: General (2 posts)
Can anyone give me a example how to do it with a one query
SELECT
`cat_name`,
(SELECT COUNT(*) FROM `posts` p WHERE p.active =1 AND p.c_id = c.cat_id) as post_count
FROM `category` c
WHERE c.parent_id = 0
ORDER BY `cat_name` ASC
try this query ,you can use subquery with select statement in query.
select cat_name,
(select count(*)
from post
where active=1
and c_id=cat_id
) as countpost
from ctagories
where parent_id=0;
How to delete all rows from a mysql table if values of two columns are equal
Example Table
invoice_id| item_id | name | invoiced_qty | received_qty
---------------------------------------------------------
| 1 | 1 | item1 | 3 | 2
| 2 | 2 | item2 | 5 | 5
| 3 | 1 | item3 | 4 | 3
| 4 | 2 | item4 | 2 | 2
| 5 | 1 | item5 | 5 | 5
After deleting table needs to retains
invoice_id| item_id | name | invoiced_qty | received_qty
---------------------------------------------------------
| 1 | 1 | item1 | 3 | 2
| 3 | 1 | item3 | 4 | 3
The select query which i created is
SELECT * FROM table1 A
INNER JOIN table1 B ON A.item_id = B.item_id
AND A.invoice_id = B.invoice_id
AND A.invoiced_qty = B.received_qty
Thanks
Why not just SQL Fiddle:
DELETE FROM table1
WHERE invoiced_qty = received_qty
Your edit does not change anything. He is the SQL Fiddle demonstrating your SELECT query. According to your sample data A.invoice_id will never equal B.invoice_id. So you will not get any results.
Try this :
DELETE FROM table1 A
INNER JOIN table1 B ON A.item_id = B.item_id
WHERE A.invoiced_qty = B.received_qty
You could simply wrap your select statement and select values to be deleted by id, like this:
DELETE FROM table1
WHERE item_id IN (SELECT item_id FROM table1 A
INNER JOIN table1 B ON A.item_id = B.item_id
AND A.invoice_id = B.invoice_id
AND A.invoiced_qty = B.received_qty)
however you should accept answer by Linger as it is more straightforward solution, mine was to indicate that if you have something selected usually you can wrap and delete.
I have a query to write and I am absolutely stumped on how to do it. Here's my situation, I am trying to provide a particular product_ID, then match all of the other product_IDs in the database that have at least the same intDescription_detail_IDs as the provided product_ID.
The relevant tables look like this:
tblproducts
=========================
product_ID | product_name
=========================
| 1 | dresser |
| 2 | bookcase |
| 3 | table |
| 4 | chair |
=========================
tbldescriptions
=========================================================================
|description_ID| intDescription_product_ID | intDescription_detail_ID |
=========================================================================
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 4 | 2 | 1 |
| 5 | 2 | 2 |
| 6 | 2 | 6 |
| 7 | 3 | 1 |
| 8 | 3 | 3 |
| 9 | 3 | 4 |
| 10 | 4 | 1 |
| 11 | 4 | 2 |
| 12 | 4 | 7 |
As an example, if I provided the product_ID "1", then I would like to return all of the product_IDs that at least have intDescription_detail_ID 1 and 2.
So, the product_IDs that should be returned are 1, 2, and 4, because all of these products have the intDescription_detail_ID of 1 and 2 among their details.
I am highly confused about how to write a query like this, so any help is greatly appreciated!
I should warn you by saying that I may have made a silly mistake here...
DROP TABLE IF EXISTS products;
CREATE TABLE products(product_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,product_name VARCHAR(20) NOT NULL UNIQUE);
INSERT INTO products VALUES
(1,'dresser'),
(2,'bookcase'),
(3,'table'),
(4,'chair');
DROP TABLE IF EXISTS product_detail;
CREATE TABLE product_detail
(product_id INT NOT NULL
,detail_id INT NOT NULL
,PRIMARY KEY(product_id,detail_id)
);
INSERT INTO product_detail VALUES
(1,1),
(1,2),
(2,1),
(2,2),
(2,6),
(3,1),
(3,3),
(3,4),
(4,1),
(4,2),
(4,7);
SELECT DISTINCT c.product_id
FROM product_detail a
JOIN product_detail b
ON b.product_id = a.product_id
AND b.detail_id <> a.detail_id
JOIN product_detail c
ON c.product_id <> a.product_id
AND c.detail_id = a.detail_id
JOIN product_detail d
ON d.product_id = c.product_id
AND d.detail_id = b.detail_id
WHERE a.product_id = 1;
+------------+
| product_id |
+------------+
| 2 |
| 4 |
+------------+
Alternative to #Strawberry’s suggestion with JOINs this can also be done using HAVING for filtering products that have (at least) the same number of rows with the same intDescription_detail_IDs as the product the search is done for:
SELECT intDescription_product_ID
FROM tbldescriptions t1
WHERE intDescription_detail_ID IN (
SELECT intDescription_detail_ID
FROM tbldescriptions t2
WHERE t2.intDescription_product_ID = 1
)
GROUP BY intDescription_product_ID
HAVING count(*) >= (
SELECT count(intDescription_detail_ID)
FROM tbldescriptions t3
WHERE t3.intDescription_product_ID = 1
)
http://sqlfiddle.com/#!2/ce698/2
One should keep in mind though that HAVING is applied last, so that will select all products with at least one matching intDescription_detail_ID first, and filter the results based on the actual count afterwards – so depending on the size and characteristic of your data set that might not be the best performing solution.