I have two tables:
I'm trying to retrieve fields name, quantity and custom_message from table 1 and join the correct associated field value from table 2 - to give me:
name | quantity | custom_message | value
This is my query:
SELECT vxu_4_wpsc_cart_contents.name, vxu_4_wpsc_cart_contents.quantity,
vxu_4_wpsc_cart_contents.custom_message
FROM vxu_4_wpsc_cart_contents
RIGHT JOIN vxu_4_wpsc_submited_form_data
ON vxu_4_wpsc_cart_contents.id = vxu_4_wpsc_submited_form_data.id
WHERE form_id =2
OR form_id =3
which is returning
name | quantity | custom_message
NULL NULL NULL
NULL NULL NULL + 3 more rows of nulls
vxu_4_wpsc_cart_contents:
table 1
vxu_4_wpsc_submited_form_data:
I just don't know where I'm going wrong!
I believe right join should be inner join. Try the following query:
SELECT
vcc.name,
vcc.quantity,
vcc.custom_message,
vfd.value
FROM
vxu_4_wpsc_cart_contents AS vcc
INNER JOIN vxu_4_wpsc_submited_form_data as vfd
ON vcc.purchaseid = vfd.log_id
WHERE
vfd.form_id IN (2,3);
(fyi: your second image link is the same as the first :))
Related
I have a table which list available items, when user click on any item it will insert in added_items table. Now my problem is I want to use join to select all items that any user has not added. My current query doesn't show items if one user has added it.
items
name | type | id | user
-------|------|----|------------------
JAVA | A | 1 | SYSTEM
PHP | A | 2 | SYSTEM
HTML | B | 3 | USER1
added_items
item_id | user
----------|--------------
1 | peter
My query
SELECT it.*
FROM items it
LEFT JOIN added_items ait
ON ait.user = it.user
#on ait.item_id = it.id
WHERE it.type = "A"
AND ait.user IS NULL
The second query I tried
SELECT it.*
FROM items it
LEFT JOIN added_items ait
ON ait.item_id = it.id
WHERE it.type = "a"
AND ait.user != "peter"
Expected result
when current user is peter I want to retrieve only PHP as peter has added JAVA.
But if current user isn't on added_items the retrieve all record.
Add fiddle http://sqlfiddle.com/#!9/3761e40
SELECT *
FROM items
WHERE NOT EXISTS ( SELECT NULL
FROM added_items
WHERE items.id = added_items.item_id
-- AND added_items.user = "peter"
)
-- AND items.type = 'A'
fiddle
You can try:
SELECT it.*
FROM items it
LEFT JOIN added_items ait
ON ait.item_id = it.id
WHERE ait.item_id IS NULL
Hi i'm trying to left join from a vessel table to daily_volume table. The two table contains the following data:
(vessel_table)
(daily_volume table)
Let's say today is 2019-07-08. I would like to left join vessel_table to daily_volume table in a way it will show this output as brand new:
My current sql statement is this but doesn't seem to achieve the above output:
SELECT * FROM vessel LEFT JOIN daily_volume ON vessel.vessel_id = daily_volume.vessel_id WHERE daily_volume.date is null
Can anyone help me how should i query to produce the output i need? Thanks
i have tried looking stackoverflow but to no avail
SELECT * FROM vessel LEFT JOIN daily_volume ON vessel.vessel_id = daily_volume.vessel_id WHERE daily_volume.date is null
vessel_id volume date
1 null null
2 null null
3 null null
4 null null
5 null null
6 null null
You seem to want a condition on the current date in the ON clause:
SELECT v.vessel_id, dv.volume, dv.date
FROM vessel v LEFT JOIN
daily_volume dv
ON dv.vessel_id = dv.vessel_id AND
dv.date = curdate();
Here is the table structure
User table
Column
fb_id
email
name
fname
lname
gender
fb_link
created
referral table
Column
id
referred_by
joinee
created
Currently implemented one SQL is (Example 1)
SELECT u.fb_id
,fb_link
,name
,r.referred_by
,u.created
FROM users u
LEFT OUTER JOIN referral r
ON u.fb_id=r.joinee
result of above query
joinee referer
10152250261037651 NULL
10152604594389921 NULL
10154430845000507 1518673071699780
....
....
....
....
547146735389782 NULL
Here is the sql (Example 2)
SELECT u_joinee.fb_id joinee_fb_id
,u_referer.fb_id referer_fb_id
,u_joinee.NAME joinee_name
,u_referer.NAME referer_name
FROM users u_joinee
,users u_referer
,referral r_j
,referral r_r
WHERE u_referer.fb_id = r_r.referred_by
AND u_joinee.fb_id = r_j.joinee
result of above query
joinee_fb_id referer_fb_id joinee_name referer_name
10154430845000507 1518673071699780 Saselsdein Bsdasd
What I am looking out for is I need both name in joinee_name and referer_name in the output. But I ma not able to use four tables in LEFT OUTER JOIN clause like i did for example 1 I want to fetch both records which include both non referral and referral joinees Is there any way to expect required result as shown below
Expected result
joinee_fb_id referer_fb_id joinee_name referer_name
10154430845000507 1518673071699780 Saselsdein Bsdasd
10154430845000347 Null asd NULL
10154430845000567 Null asd asdm NULL
10154230845000567 Null Dsd asdm NULL
10154330845000567 Null sdm NULL
101544553045000567 Null Esd aedm NULL
You can join with the users table twice by giving it different aliases, just like you do with your implicit inner join.
I don't think you need to join with referral twice. A single row in the referral table links a referrer and a joinee.
SELECT u_joinee.fb_id joinee_fb_id ,u_referer.fb_id referer_fb_id, u_joinee.name joinee_name, u_referer.name referer_name
FROM users AS u_joinee
LEFT JOIN referral AS r ON r.joinee = u_joinee.fb_id
LEFT JOIN users AS u_referer ON r.referred_by = u_referer.fb_id
I have two tables reports & viewedids
the first one stores data and the second one stores user IDs that viewed data:
viewedids:
+-------+------+
| uid | rid |
+-------+------+
| 2 | 5 |
+-------+------+
each (uid,rid) means the uid has viewd rid
I want to select * from reports table and add view state (0 or 1) for current user to it. (A JOIN statement)
How can I do this?
You can use a LEFT JOIN:
SELECT reports.*, viewedids.uid IS NOT NULL as view_state
FROM
reports LEFT JOIN viewedids
ON reports.id = viewedids.rid
AND viewedids.uid = #current_user
This will return all reports, and will try to join reports table with viewedids ON reports.id = viewedids.rid AND viewedids.uid = #current_user. If the join succedes, viewedids.uid will be not null, and viewedids.uid IS NOT NULL will be evaluated to 1. It will be evaluated 0 otherwise.
Please see fiddle here.
I have two databases: one contains the users:
persons
id
name
course
the other one contains the
voters
id
personID
type
An entry to the voters table gets added when the user voted (type describes different topics a user can vote on)
I want to get all users for example where persons.course = '1' AND voters.type = '2' and have the output contain the following columns: id, name, hasVoted (where hasVoted returns true if the personID can be found in the voters table)
A left join may be what your looking for.
SELECT persons.id, persons.name, voters.personID IS NOT NULL AS hasVoted
FROM persons LEFT JOIN voters ON (persons.id = voters.personID)
WHERE persons.course = '1' AND voters.type = '2'
GROUP BY persons.id
http://dev.mysql.com/doc/refman/5.0/en/join.html
The left join will include all the matching rows from the left side of the join, and any matching pieces from the right side of the join. Any rows from the left side without a matching right side will be have the right side columns values of null.
As for the 'IS NOT NULL' or 'IS NULL' is used because 'X = NULL' doesn't behave as it appears.
> SELECT 'A' = NULL, 'A' IS NULL, NULL = NULL, NULL IS NULL;
+------------+-------------+-------------+--------------+
| 'A' = NULL | 'A' IS NULL | NULL = NULL | NULL IS NULL |
+------------+-------------+-------------+--------------+
| NULL | 0 | NULL | 1 |
+------------+-------------+-------------+--------------+
More details about working with nulls:
http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html