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 8 years ago.
Improve this question
I want to built a system where users list their used books. I have built two independent tables and a lookup table to match the users with their listed books.
Login/Register system is completed. A CRUD system for the books was also built.
Now I want to give edit/delete permission only to the book owner but I have no idea how to match them w/o problem. How can I do that?
user_table
----------------------------
| UserID | Username | ... |
----------------------------
| 1001 | Deniz | ... |
----------------------------
| .... | ..... | ... |
----------------------------
book_table
----------------------------
| BookID | Bookname | ... |
----------------------------
| 5123 | SQL NOW | ... |
----------------------------
| ... | ... | ... |
----------------------------
user_book_table
--------------------
| UserID | BookID |
--------------------
| 1001 | 5123 |
--------------------
| ... | ... |
--------------------
SELECT * FROM user_table AS u, book_table AS b
LEFT JOIN user_book_table USING(UserID)
WHERE u.UserID = 1001
AND b.BookID = 5123;
With this structure you can simply check if the book is owned by a user. If so, he/she may edit the book.
Related
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 7 months ago.
Improve this question
I have four different table. And invoice_id is foreign key in other table.
i want to show all records against invoice_id.
invoice_id | Bill_amount | Commission_amount | Payment_amount |
| | | |
2 | ------ | 1000 | 500 |
2 | ------ | 200 | 100 |
2 | ------ | -------------- | 100 |
2 | ------ | ------------ | 50 |
3 | 100 | 200 | -------- |
And So On........
Suppose you need 4 columns from 4 different tables. You can use left join if you want to include the empty records also from table2, table3 and table4.
Try the following query:
SELECT
table1.invoice_id,
table2.Bill_amount,
table3.Commission_amount,
table4.Payment_amount
FROM
table1
LEFT JOIN table2 ON table1.invoice_id = table2.invoice_id
LEFT JOIN table3 ON table1.invoice_id = table3.invoice_id
LEFT JOIN table4 ON table1.invoice_id = table4.invoice_id
WHERE
table1.invoice_id = '(your_required_id)';
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.
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
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 8 years ago.
Improve this question
Database 1 : sandbox
table 1 : coordinates
+----------------------------------------+
|coord_id | section_name | station_number|
+----------------------------------------+
| 1 | A | A7 |
| 2 | B | B20 |
| 3 | C | C3 |
| 4 | D | D14 |
| 5 | E | E9 |
+----------------------------------------+
database 2 : phone
table 2 : workstations
I only have READ privilege
+----------------------------+
| ID | ws | pod |
+----------------------------+
| 1 | COMP123 | A07 |
| 2 | COMP345 | B20 |
| 3 | COMP567 | C03 |
| 4 | COMP891 | D14 |
| 5 | COMP444 | E09 |
+----------------------------+
PROBLEM:
I only have READ privilege on that second table of that database.
I want to join both tables so I can display the "ws" field on screen for my PHP script.
My "station_number" field values are written differently from the "pod" field
(they have a zero in front of the letter if it is a single digit after the letter) does it make a difference?
I've seen examples online on how to join but for some reason I can't get it to work.
Do I need to create an extra field on my main Table to store the field values from "ws" or what? I'm a bit confused.
Thanks in advance!
Yes, it makes a difference if there's a 0 in one and not in the other. You'll have to modify the value of one or the other. Something like SUBSTR(station_number, 0, 1) + LPAD(SUBSTR(station_number, 2), 2, '0').
And to join the tables from different databases, you just have to put the database name in front of the table name. Probably something like sandbox.coordinates.
So your query might look something like this:
SELECT
*
FROM
sandbox.coordinates c
INNER JOIN phone.workstations w
ON (SUBSTR(c.station_number, 0, 1) + LPAD(SUBSTR(c.station_number, 2), 2, '0')) = w.pod
If you can update one of your tables to make the columns match, then your query is as simple as:
SELECT
*
FROM
sandbox.coordinates c
INNER JOIN phone.workstations w
ON c.station_number = w.pod
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In the following tables in mysql of users following/being subscribed to other users (related in th 'follow' table) I would like to get the posts posted by the users to which a given user is subscribed:
table 1: follow
|-----------------------------------------|
| id | uid | friends |
|-----------------------------------------|
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 2 | 1 |
|-----------------------------------------|
table 2: posts
|-----------------------------------------------------|
| id | uid | posts | date |
|-----------------------------------------------------|
| 1 | 1 | hai.. | 2013-07-08 01:56:09 |
| 2 | 5 | awesome | 2013-07-08 11:45:50 |
| 3 | 2 | greate!! | 2013-07-09 21:13:29 |
| 4 | 3 | himm.. | 2013-07-10 12:06:10 |
| 5 | 2 | super.. | 2013-07-10 14:50:09 |
|-----------------------------------------------------|
table3: user
|---------------------------|
| uid | name |
|---------------------------|
| 1 | ram |
| 2 | syed |
| 3 | seeta |
|---------------------------|
For example:
Given the user with uid 1, who follows both users with uid 2 and 3, I would like to display my posts and the latest posts of the followes.
The result would look like this:
2 posted
super..
time:2013-07-10 14:50:09
3 posted
himm..
time:2013-07-10 12:06:10
2 posted
greate!!
time:2013-07-09 21:13:29
1 posted
hai..
time:2013-07-08 01:56:09
Suppose your user id is 5:
SELECT * FROM posts WHERE posts.uid IN (SELECT follow.friends FROM follow WHERE follow.uid=5)
Or you can join the tables:
SELECT posts.* FROM posts JOIN follow ON posts.uid = follow.friends WHERE follow.uid=5
If you want to see your own posts as well:
SELECT * FROM posts WHERE posts.uid=5 OR posts.uid IN (SELECT follow.friends FROM follow WHERE follow.uid=5)
I'm not really sure if your tables are in sql, but in case they are, I think the best way to get the data as you want is with a LEFT JOIN as follows:
SELECT * FROM follow
JOIN posts
ON (follow.friends = posts.uid OR follow.uid = posts.uid)
WHERE follow.uid = {user_uid}
Where {user_id} is the user id you want.
If this is the case, please take a further look to joins (f.e. at tizag) and left joins.