I have four tables in MySQL.
+-----------------------------------------------+
Table 2 | Table 1 |
+============+=========+========+============+ +==========+=====================+
| ID | Name | Add | d_id | | ID | Details |
+============+=========+========+============+ +==========+=====================+
| 1 | ABC | City | 3 | | 1 | blah blah blah |
+------------+---------+--------+------------+ +----------+---------------------+
: : : : : : : :
|
+-------------------------+
|
Table 3 | Table 4
+============+==========+============+ +=========+======================+
| d_id | dis_Name | cat_id |----+ | cat_id | category_name |
+============+==========+============+ | +=========+======================+
| 1 | Myeloma | 5 | | : : :
+------------+----------+------------+ | +---------+----------------------+
: : : : +----| 5 | Cancer |
+---------+----------------------+
What I want to do is, I want to select all the rows in 'Table 1' those belongs to the 'category_name' in 'Table 4'.
Table 2 may contain multiple rows having same 'd_id'. Likewise, Table 3 may have multiple rows having same 'cat_id'.
If I select Category 'Cancer' then I would be getting multiple d_id's from table 3 in result. For each d_id, there will be multiple ID's in table 2. I want to fetch the details of these ID's (from Table 2) from Table 1. What would be the query statement???
Connectivity is shown in the table representaion.
you can achieve it doing this:
SELECT t1.*
FROM t4
INNER JOIN t3 ON (t4.cat_id = t3.cat_id)
INNER JOIN t2 ON (t3.d_id = t2.d_id)
INNER JOIN t1 ON (t2.id = t1.id)
WHERE t4.category_name = 'Cancer';
What you need is a join.
SELECT table.column_in_table
FROM table
INNER JOIN table_with_same_values
ON table.column_in_table = table_with_same_values.column_in_that_table;
I'm still not clear what you're trying to ask. Your question is a bit verbose. Some additional info here.
Hope this helps!
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 two tables in my SQL Server database. The first is catgeories and second is products. There is a column categories_id in both tables.
HERE IS MY TABLE FOR CATEGORIES :
+----+----------+----------+
| id | category | parent |
+----+----------+----------+
| 1 | BOY | 0 |
| 2 | GIRL | 0 |
| 3 | SHIRT | 1 |
| 4 | SKIRT | 2 |
| 5 | JACKET | 1 |
+----+----------+----------+
TABLE : PRODUCTS
+-------+--------------+----------------------+
| id | title |PRICE | Categories |
+-------+--------------+------+---------------+
| 1 | RED SHIRT | 300 | 3 |
| 2 | blue SKIRT | 500 | 4 |
| 3 | jeans jacket | 500 | 3 |
+-------+--------------+------+-----+---------+
Now I want to select the values from Products table for a particular category like BOY.
Try This:
SELECT pr.id,pr.title,pr.price from products AS pr
INNER JOIN CATEGORIES AS cat ON cat.id=pr.Categories
WHERE cat.category='Boy';
SELECT products.* FROM products INNER JOIN categories ON products.categories = categories.id WHERE categories.category LIKE '%BOY%';
Use this query..
Either
SELECT * FROM tproducts WHERE categories = 1
or
SELECT * FROM tproducts
JOIN tcategories ON tcategories.id = tproducts.categories WHERE tcategories.category = 'BOY'
I don't know your table names so I just used tproducts and tcategories
There is no boy(1) ID in Categories(field) in PRODUCTS(table)
SELECT * FROM CATEGORIES
INNER JOIN PRODUCTS
ON CATEGORIES.id=PRODUCTS.Categories
WHERE CATEGORIES.category ='BOY'
FOR this use join query Example:
select *(or you can get any column as you write name) from table1 join table2 on table1.id=table2.id where table1.category='BOY';
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
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 having two tables
table 1: users
| id | username |
| 1 | john |
| 2 | marry |
| 3 | deep |
| 4 | query |
| 5 | value|
and
table 2:users_2
| table_2_id | user_id |
| 1 | 2,4 |
I need required something like this
| table_2_id | username |
| 1 | marry,query |
anyone can help me for this output in mysql
Is this what you are looking ?
select
`users_2`.`table_2_id` , GROUP_CONCAT(`users`.`username`) as `usernames`
from `users_2`
inner join `users` on FIND_IN_SET(`users`.`id`,`users_2`.`user_id`)
Check output here
http://sqlfiddle.com/#!2/c498bc/3
select a.table_2_id,b.username
from users b,users_2 a
where a.table_2_id=b.id
and b.id in(a.user_id)
group by a.table_2_id
First of all, you should not store a multiple value in a single field. For table users_2, the data should be:
table_2_id user_id
1 2
1 4
After you normalized your table, you can use mysql GROUP_CONCAT() to get the result in the format you mentioned
SELECT
users_2.table_2_id,
GROUP_CONCAT(users.username) AS username
FROM
users_2
JOIN
users ON users.id = users_2.user_id
GROUP BY
users_2.table_2_id
;