mysql - many to many query from bridge table [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
i have a task to complete. there is a many to many relationship. the bridge table has been made which looks like
left id right id
+----------+---------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 2 | 8 |
| 3 | 1 |
| 3 | 2 |
| 3 | 4 |
| 4 | 1 |
| 4 | 2 |
| 4 | 3 |
| 4 | 5 |
| 5 | 1 |
| 5 | 2 |
| 5 | 4 |
| 5 | 6 |
| 5 | 7 |
+----------+---------+
i have to display the left id = right id in one row
for example
for left id 1
left1 | right1 righ 2
for left id 3
left3 | right1 right2 right 4
how do i do this ? i have tried joining table , doesn't work

I think you can use a simple query to acheive this using GROUP BY and GROUP_CONCAT()
SELECT left_id, GROUP_CONCAT(right_id SEPARATOR ' ') as rigth_id
FROM left-right
GROUP BY left_id;

This is a reasonably straightforward application of GROUP_CONCAT() and GROUP BY. (http://sqlfiddle.com/#!9/ed7e1/2/0)
SELECT leftId,
GROUP_CONCAT(rightId ORDER BY rightId) rightIds
FROM bridge
GROUP BY leftId

Related

sum and group display in mySql PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
I have a table below.
-----------------------------------------------
id | items | user | department | value | date |
-----------------------------------------
1 | books | john | dept-1 | 5 | 1-1-20
-----------------------------------------
2 | fruits | john | dept-1 | 10 | 3-1-20
-----------------------------------------
3 | books | rehman | dept-2 | 8 | 1-1-20
-----------------------------------------
4 | fruits | rehman | dept-2 | 20 | 9-1-20
-----------------------------------------
5 | woolens | john | dept-1 | 15 | 1-1-20
-----------------------------------------
6 | woolens | vinay | dept-1 | 9 | 3-1-20
-----------------------------------------
7 | veggies | sunil | dept-2| 20 | 10-1-20
-----------------------------------------
now I want to display these items like
department | user | value
--------------------------
dept-1 |
| john | 30
| vinay | 9
dept-2 |
| rehman | 28
| sunil | 20
how can I achieve this in mysql, this will be call in ajax and display in a table by php.
can someone help pls.
I tried doing grouping and used while loop but not able to execute desired output.

How to compare two tables and show every difference? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have 2 table with diferent data
Finance Orderan
+-----------------+ +---------------+
| id | no_order | | id| no_order |
+-----------------+ +---------------+
| 1 | 1234567890 | | 1 | 1234567890|
| 2 | 0987654321 | | 2 | 0987654321| |
| 3 | 1122334455 | | 3 | 1122334455|
| 4 | 1212121212 | | 4 | 2222222222|
+-----------------+ +---------------+
I want to return diferent data like this:
+-------------------------+
| no_order | no_order |
+-------------------------+
| 1212121212 | 2222222222 |
+-------------------------+
Maybe you need in simple
SELECT t1.no_order, t2.no_order
FROM Finance t1
JOIN Orderan t2 ON t1.id = t2.id
WHERE t1.no_order != t2.no_order
?

Remove assigned users from database table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a table:
| staff_id | leader_id | team_role | importance |
+ + + + +
| 1001037 | 1000321 | HOD | 1 |
| 1001037 | 1000322 | HOD | 1 |
| 1001037 | 1001037 | Supervisor | 2 |
| 1001094 | 1001037 | Checker | 3 |
| 1001075 | 1001037 | Checker | 3 |
| 1001096 | 1001075 | Squad Leader | 4 |
| 1000393 | 1001094 | Squad Leader | 4 |
| 1000465 | 1001094 | Squad Leader | 4 |
| 1000585 | 1001075 | Squad Leader | 4 |
| 1000664 | 1000585 | Team Member | 5 |
| 1000583 | 1000585 | Team Member | 5 |
| 1000570 | 1000465 | Team Member | 5 |
| 1000316 | 1000465 | Team Member | 5 |
In php it look like this:
If I want to remove user with staff_id = 1001075 I need to remove all assigned to this user ids. So it will be ids where 1001075 is leader_id. I am able to this but I also need to remove users going down to the tree so where leader_id= 1001075 is will be 1000585 Then I need to remove users where leader_id= 1000585
In the end I need to delete these users with staff_id:
1001075
1001096
1000585
1000664
1000583
How can I do that?
There are two ways to do this:
Recursively query for all the ids to find out all the children and delete the rows one by one. Have a look at this answer for approaches to recursively query the ids.
Run an ALTER TABLE script and make leader_id a foreign key, referencing to staff_id of the same table, and define with ON DELETE CASCADE. By this way, when you delete a leader row, it will delete all the subsequent rows, e.g.:
ALTER TABLE table
ADD CONSTRAINT fk_leader_id FOREIGN KEY (leader_id) REFERENCES table(staff_id) ON DELETE CASCADE;
I would prefer the second approach.

Code to fetch Data from 2 tables & enter in 3rd Table in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Table1 - Payout Requests
`
|--------|-----------|-----------|
| id | Receiver | Balance |
|--------|-----------|-----------|
| 1 | user1 | 2000 |
|--------|-----------|-----------|
| 2 | user2 | 1500 |
|--------|-----------|-----------|`
Table2 - Donars
`
|--------|-----------|-----------|
| id | Donar | Amount |
|--------|-----------|-----------|
| 1 | love | 1500 |
|--------|-----------|-----------|
| 2 | don1 | 1000 |
|--------|-----------|-----------|`
Now my question; What piese of code will make Adjustments of both the tables so that data from these two tables goes in 3rd table in the following way
Table3 - Links
`
|--------|-----------|-----------|----------|
| id | Donar | Receiver | Amount |
|--------|-----------|-----------|----------|
| 1 | love | user1 | 1500 |
|--------|-----------|-----------|----------|
| 2 | don1 | user1 | 500 |
|--------|-----------|-----------|----------|
| 3 | don1 | user2 | 500 |
|--------|-----------|-----------|----------|`
AND now Table 1 & Table 2 will be Follows -
Table1 - Payout Requests
`
|--------|-----------|-----------|
| id | Receiver | Balance |
|--------|-----------|-----------|
| 2 | user2 | 1000 |
|--------|-----------|-----------|`
Table2 - Donars
`
|--------|-----------|-----------|
| id | Donar | Amount |
|--------|-----------|-----------|
| Nothing |
|--------|-----------|-----------|`
In the first step you should have a foreign key on Donars table which save id of Payout Requests. for example pr_id
You can use INNER JOIN like this:
SELECT `t2`.`id`, `t2`.`Donar`, `t1`.`Receiver `, `t1`.Balance - `t2`.`Amount` AS `Adjustment` FROM `table1` AS `t1` INNER JOIN `table2` AS `t2` ON `t2`.`pr_id` = `t1`.id

Search for time in mysql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This id my table :
+------------+--------------+---------------------+--------+
| id | playduration | time | id_his |
+------------+--------------+---------------------+--------+
| 129 | 00:00:00 | 2014-01-19 04:08:00 | 1 |
| 190 | NULL | 2014-01-19 02:20:34 | 6 |
| 1390228962 | NULL | 2014-01-19 02:09:20 | 4 |
| 1390228963 | NULL | 2014-01-19 02:06:12 | 3 |
| 188 | NULL | 2014-01-19 02:00:11 | 5 |
| 151 | NULL | 2014-01-19 01:06:01 | 7 |
| 195 | NULL | 2014-01-15 01:10:00 | 2 |
+------------+--------------+---------------------+--------+
and i want to get row id = 1390228963 when I search for time = 2014-01-19 02:07:00
I thought that I have been explained it good
You need to get the last row before the time:
select t.*
from table t
where time <= '2014-01-19 02:07:00'
order by time desc
limit 1;
Just use this mysql query
SELECT * from tablename where time="2014-01-19 02:00:11";

Categories