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
let say we've two tables as following
Table Users
+----+----------+-------+
| id | username | email |
+----+----------+-------+
| 1 | user1 | mail1 |
| 2 | user2 | mail2 |
| 3 | user3 | mail3 |
| 4 | user4 | mail4 |
+----+----------+-------+
Table sales
+----+----------+-------+
| id | username | email |
+----+----------+-------+
| 1 | user1 | mail1 |
| 2 | user3 | mail3 |
+----+----------+-------+
I wanna print the emails of users where their username not found at sales table as if I wanna say select email from users where username not found in table sales
Output should be like this
email2
email4
Please note that i'm usring mysql ~ thanks
There are many possible solutions on this problem. One is by using LEFT JOIN. Any record that has no matching record on the right hand side (Sales) table will have a value of null on its columns which you can filter with.
SELECT a.email
FROM Users a
LEFT JOIN Sales b
ON a.username = b.username
WHERE b.username IS NULL
SQLFiddle Demo
select users.* from users left join sales on sales.username = users.username
where sales.username is null
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 debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have three tables:
Table 1 contains a primary key.
Table 2 contains a foreign key that is equal to primary key of table 1.
Table 3 contains a foreign key that is equal to primary key of table 2.
QUESTION: Is it possible to SELECT information from table 2 and 3 while only knowing the primary key of table 1? If so please give a brief example of the SELECT QUERY. I have tried doing an Inner join but got a syntax error.
Yes you can.
Example:
table_a table_b table_c
_______________ _______________ _______________
| id | name | | id | gender | | id | age |
|------+--------| |------+--------| |------+--------|
| 1 | sam | | 1 | m | | 1 | 18 |
|------+--------| |------+--------| |------+--------|
| 2 | ana | | 2 | f | | 2 | 22 |
|------+--------| |------+--------| |------+--------|
In order to get the following result:
_________________________________
| id | name | gender | age |
|------+--------+--------+--------|
| 1 | sam | m | 18 |
|------+--------+--------+--------|
| 2 | ana | f | 22 |
You could use the following SQL statement:
SELECT a.id, a.name, b.gender, c.age
FROM table_a AS a
LEFT JOIN table_b AS b
ON a.id = b.id
LEFT JOIN table_c AS c
ON a.id = c.id
P.S.: only answered this to do the ascii art xD!
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 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.
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.