我有一个问题,当我执行一个sql语句时,当任何行在某一列中具有空值时,它不会取任何行。
我的sql语句如下:
SELECT * FROM cb_users u
LEFT JOIN Story1 s ON s.ID = u.user_id
LEFT JOIN cb_users_pitches p ON u.user_id = p.user_id
WHERE u.user_id NOT IN (SELECT user_id FROM cb_contact_tickets WHERE ticket_status IN ('open', 'unresponsive', 'declined')) AND role = 'filmmaker' AND u.status = 'idle'
ORDER BY u.last_login
我认为在我检查user_ids以防止可能为null的用户ID时,有一些事情要做。任何可能的解决方法?
当cb_contact_tickets表中的user_id中的行具有空值时,包含空值时,绝对没有任何内容
没有空值时的示例数据:
user_id, role, status, last_login, login_count, signed, joined, longform, signed_ip, signed_dt, user_score, fav_director, fav_film, fav_act, board_member, votes, about_me, has_image, invite_date, vimeo_id, access_token, access_token_secret, vimeo_group, pm_notify, join_process, join_step, future_role, primary_group, unidle_date, conversion, previous_role, points, ID, Email, FirstName, LastName, Password, nCity, primary_country, primary_city, temporary_password, Status2, Status, Last_Contact, Next, Comments, Rank, Notes, Directed, Nar_List, Nar_Links, Other_List, Other_Links, Interest, Criticism, Internet, City, Available, Heard, Updated, Joined, Contact, Agreement, TimeZone, ChatTime, signname, signdate, lastlogin, numlogins, lastIP, Pitch1, nobudget, GroupNo, LinkPop, P2Late, Pnote, New, DueDate, Note, ShowDD, PastDue, Reading, PDDate, DaysLate, Cities, c3login, c3logins, dwdate, tnote, date_applied, pitch_id, pitch, user_id, dt, deleted, page, light
'515', 'filmmaker', 'idle', '2013-04-27 04:06:18', '5', '2013-04-16 01:55:00', 'accept', 'accept', '122.176.139.62', '2013-04-16 02:19:44', '0', '', '', '', '1', '0', '', 'no', '2013-01-19 12:59:51', '', '', '', 'no', 'on', '1', '5', 'filmmaker', '9', NULL, NULL, NULL, NULL, '515', 'anurag.sikder@gmail.com', 'Anurag', 'Sikder', '69b0f8b481b30559d4ca1701e33762104f4f66a1', 'India', '', '', 'no', 'billion', 'NEW', '0000-00-00', 'so should he be in new? or are we waiting for more? put him in new if he should be.\r\n\r\n12.22 i think i mis-communicated a little. he wasn\'t finished with what i said below that he was going to send..so probably he should have been in new-i e\'d him again-cjs\r\n\r\nif he\'s about to send more then wait for it til we make him new so he gets reviewed once.. - mcs\r\n10.5 I found his links in the old feedback form but he still says he will be sending his newer work-cjs\r\n9.20 e\'d for film -cjs\r\nASK HIM TO SEND HIS FILM -- last time said he was finishing it.\r\nRESPONDED TO REVISIT EMAIL\r\n\r\nwas finishing his film. revisit later or if he uploads it. \r\n1.24 didn\'t get email, resent again\r\n1.18: first email. he said would upload. ', '', '0', '', 'Yes', 'I have till now directed only on film called Self-Service. The film was completed in December. Till now, it has not been screened anywhere yet.', 'Director Links: 1. Self Service- http://www.youtube.com/watch?v=axkfK5qg8Z0\r\n2. All Shook Up- http://youtu.be/rY98e-ZYYks\r\n', '', '', 'The opportunity to learn more about cultural and methodological differences between directors from different parts of the globe. The chance to have fun and create something in collaboration with so many creative minds.', 'Yes if it is founded on honest and clear reason. ', 'Yes', 'India', 'January End', 'Facebook', '2013-01-19 12:59:57', '0000-00-00', '', 'False', NULL, NULL, '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '', '', 'Yes', '0', '0', 'FALSE', NULL, NULL, '0000-00-00', '', 'TRUE', 'FALSE', '0', '0000-00-00', '0', 'In India, New Delhi, Haryana, West Bengal, Andhra Pradesh and Maharashtra.', '0000-00-00 00:00:00', '0', '0000-00-00', '', '0000-00-00 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL
'522', 'filmmaker', 'idle', '2013-04-09 10:17:18', '2', '2012-09-17 19:08:29', 'accept', 'accept', '186.80.98.141', '2012-09-17 19:14:17', '0', 'Emir Kusturica', 'The mirror (Tarkovsky)', 'Daniel Day - Lewis', '1', '0', 'www.vimeo.com/18461027 (As actor - Ganster and director of actors)\r\n\r\nhttp://www.youtube.com/watch?v=Vl07w4CBEN8 (As director)\r\n\r\nhttp://www.youtube.com/watch?v=', 'no', '0000-00-00 00:00:00', '', '', '', 'no', 'on', '1', '5', 'filmmaker', '9', NULL, NULL, NULL, '1575', '522', 'andres9filmscolombia@hotmail.com', 'Andrés', 'Sandoval', '6482e88cf393c0667faa74bb2a109ab9531102b0', 'Bogota, Colombia', '', '', 'no', 'Active', 'NEW', '0000-00-00', 'new email: andres9filmscolombia@hotmail.com\r\nold email: andresando_23@hotmail.com\r\n\r\nprompted 1.27\r\ninviting 1.23', 'I: This is Yango\\\'s \\\"Actor\\\'s Director\\\" he has two teasers for this whacked out horror movie and co-directed a fairly slick looking music video.', '0', '', 'Yes', 'MARINA\\\'S HUSBAND (Director, Writer and Producer)\r\nRESURRECCJA (Resurrection) (Writer, Producer and CoDirector with Yango Gonzalez)\r\nOMEGA EFFECT (Teaser for TV) (Writer, Director and producer)\r\nFILE 0 (Pilot of Omega Effect -TV series) (Writer, Director and producer)\r\nPOKER (Feature)( Actor\\\'s director) \r\nEL DORADO INSANE ASYLUM (Actor\\\'s Director- Actor)\r\nTHE IMPORTANCE OF A MINUTE (Assistant director)\r\nMEMORIES (Cameraman)\r\n', 'http://www.youtube.com/watch?v=TARV8OWTFKA\r\nhttp://www.youtube.com/watch?v=Vl07w4CBEN8\r\nhttp://www.youtube.com/watch?v=SDT2IJXZgdo&feature=related', 'SOBREVIVIENTES- SURVIVORS (VideoClip Group Las Plagas) (Writer, CoDirector With Mateo Stivelberg)\r\n\r\n', 'http://www.youtube.com/watch?v=GXjXKOmvtiQ', 'I\\\'d like to tell a good story in a short time', 'Yes', 'Yes', 'Bogota, Colombia', 'Now', 'Yango Gonzalez', '2012-02-04 16:33:40', '0000-00-00', '', 'False', NULL, NULL, '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '190.255.243.89', '', 'Yes', '0', '0', 'FALSE', NULL, NULL, '0000-00-00', '', 'TRUE', 'FALSE', '0', '0000-00-00', '0', 'Around Bogota', '2013-08-22 21:17:00', '411', '0000-00-00', '', '0000-00-00 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL
'657', 'filmmaker', 'idle', '2013-06-19 15:16:33', '38', '2012-09-12 09:43:58', 'accept', 'accept', '89.136.169.251', '2012-09-13 04:24:20', '0', 'Welles, Tarr, Hitchcock', 'Pociag, Suna no onna', 'Peter O\\\'Toole', '1', '0', '', 'no', '0000-00-00 00:00:00', '', '', '', 'no', 'on', '1', '5', 'filmmaker', '9', NULL, NULL, NULL, '3207', '657', 'atgraph@gmail.com', 'Adrian', 'Tudor', '057f4b9ac7f3a4465a8013cce98775b6784c3168', 'Romania', '', '', 'no', 'Active', 'NEW', '0000-00-00', '8.15 he\\\'s been logging in, emailed him\r\n5.29 he\\\'s been under-studying Act 3\\\'s/Refrains.. told him it would be a while, slight prompt for him to pick a main story gap.\r\n3.6 pre-welcomed, told prob bring in in April. ', 'great stuff, not as narrative as exprimental and noir-ish black and white and concepty, image-based. A little wim wenders on the trailer to curtain. ', '0', '', 'Yes', '2007 - Anoxia - Director, Writer, Sound, Editor\r\n2008 - On the Same Road - Director, Writer, Music, Editor\r\n2008 - Quasi Nihil II - Director, Writer, Music, Editor\r\n2009 - House of Veils - Director, Writer, Music, Editor\r\n2010 - Curtain - Director, Writer, Music, Editor', 'www.adriantudor.com/film.html', 'As director:\r\n...sur tonalite - 2007 - Documentary with Gheorghe Zamfir\r\n#11 - 2008 - Music Video\r\n', '', 'The diversity of ideas that can be found when filmmakers from all over the world are gathered together.', 'Yesy', 'Yes', 'Romania', 'March', 'Search Engine', '2011-08-15 11:33:06', '0000-00-00', '', 'False', NULL, NULL, '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '89.136.169.251', '', 'Yes', '0', '0', 'FALSE', NULL, NULL, '0000-00-00', '', 'TRUE', 'FALSE', '0', '0000-00-00', '0', 'Any other city in Romania', '2013-08-28 04:15:17', '541', '0000-00-00', '', '0000-00-00 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL
'732', 'filmmaker', 'idle', '2013-04-23 21:01:18', '24', '2012-09-19 01:27:30', 'accept', 'accept', '218.186.19.10', '2012-09-19 04:00:25', '0', 'Akira Kurosawa ', 'Rashomon ', 'Leung Chew Wai', '1', '0', 'I am a home tutor, writer of science assessment books and filmmaker. So far, I have made 5 short films of various genres from silent comedies to period wuxia (Chinese martial arts). ', 'no', '0000-00-00 00:00:00', '', '', '', 'no', 'on', '1', '5', 'filmmaker', '9', NULL, NULL, NULL, '1560', '732', 'tingszukiong@gmail.com', 'Szu Kiong ', 'Ting', '8a79f4d72cc10b414e55db554f112de27701051c', 'Singapore', '', '', 'no', 'billion', 'NEW', '0000-00-00', '8/20: dropped out, thought he would have shot by now and had work.. willing to come back later. \r\nEmailed - 3/15 - told him we would contact him the next time we need someone.\r\nVery professional Asian TV stuff with a good amount of creativity. ', '', '0', '', 'Yes', 'MY KEYS (8min) \r\n(Writer/Director)\r\n- Nominated for Best Short Film in 4th Asian Festival of First Films \r\n\r\nMICHELLE (8min)\r\n(Director)\r\n- Selected for Asian Film Symposium \r\n\r\nTHE TRIUMPH (8min)\r\n(Writer/Director)\r\n- Won Best Sound award in Singapore Management University Film Festival \r\n\r\nTHE FOREST SPIRITS (10 min) \r\n(Writer/Director)\r\n- Curator\\\'s Pick in Maskara Shorties \r\n- Curator\\\'s Pick in Sinema Showoff The Oriental Show \r\n- Nominated for Best Sound in 2nd Singapore Short Film Awards', '- http://www.cultureunplugged.com/documentary/watch-online/filmedia/play/607/My-Keys\r\n- http://vimeo.com/14940649 (password: wuxia)', '', '', 'I have always wanted to be acquainted with other filmmakers around the world and look out for opportunities to work or collaborate with them. I believe that independent filmmakers who are aspiring to do feature-length films but are lack of funds should work together in some way or another. Recently I have brought together three filmmakers including me to brainstorm on a feature-length film directed by three different directors. One of them is the German lady director, Juliane Block. When she told me of collabfeature during the discussion, I am immediately thrilled. ', 'Yes', 'Yes', 'Singapore', 'Immediately', 'From an existing member, Juliane Block', '2012-09-18 13:22:26', '0000-00-00', '', 'False', NULL, NULL, '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0', '218.186.19.10', '', 'Yes', '0', '0', 'FALSE', NULL, NULL, '0000-00-00', '', 'TRUE', 'FALSE', '0', '0000-00-00', '0', 'Malaysia, Bangkok, Hong Kong, China ', '2011-08-13 03:12:26', '27', '0000-00-00', '', '0000-00-00 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL
来自contact_tickets表的示例数据
# ticket_id, captain_id, user_id, date_opened, date_closed, ticket_status, comments, group_id, pm_sent, task, declined
'94', '1733', '1718', '2013-08-24 12:44:05', NULL, 'open', 'Contacted on 24/08/2013', '3', '0', '', ''
答案 0 :(得分:3)
有三种方法最好选择一个表中存在但另一个表中缺少的值。它们如下:
观察你的情景,最好避免使用三价的NOT IN
,i。即它可以返回TRUE,FALSE或NULL。 <{1}}或NOT EXISTS
将是您理想的匹配。
了解更多信息:http://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/
答案 1 :(得分:2)
这是NOT IN
和SQL的三元逻辑的问题。请参阅以下详细说明: SQL NOT IN not working
解决方案是将NOT IN
更改为NOT EXISTS
。 WHERE
子句:
WHERE u.user_id NOT IN
( SELECT user_id
FROM cb_contact_tickets
WHERE ticket_status IN ('open', 'unresponsive', 'declined')
)
AND role = 'filmmaker'
AND u.status = 'idle'
变为:
WHERE NOT EXISTS
( SELECT 1
FROM cb_contact_tickets AS t
WHERE t.ticket_status IN ('open', 'unresponsive', 'declined')
AND t.user_id = u.user_id
)
AND role = 'filmmaker'
AND u.status = 'idle'