SQL AND,看看是否在两个不同的表中设置了两个标志?

时间:2019-02-28 19:21:52

标签: sql db2

SELECT 1 FROM
(SELECT 1 FROM mytable1 WHERE parentid = 'ID1' AND flag = 'Y') as X,
(SELECT 1 FROM mytable2 WHERE id = 'ID2' AND flag = 'Y') as Y 

我要查询两个表中是否设置了两个标志,其中“ parentid”和“ id”都是主键。仅当两个标志都设置为“ Y”时,查询才应返回一行,否则,则不返回任何内容,然后在后端代码中对该结果进行处理。

我已经对此进行了测试,但效果不错,可以优化。有什么想法吗?

3 个答案:

答案 0 :(得分:2)

获得想要的东西:

SELECT 1
    FROM mytable1 AS a, mytable2 AS b
    WHERE a.parentid = 'ID1' AND a.flag = 'Y'
        AND b.id = 'ID2' AND b.flag = 'Y' 

但是实际上,我更喜欢使用LEFT JOIN的查询,该查询总是给出一行,就像这样:

SELECT CASE WHEN a.flag = 'Y' AND b.flag = 'Y' THEN 1 ELSE 0 END AS result
    FROM TABLE ( VALUES 1 ) AS always(present)
        LEFT JOIN mytable1 AS a ON a.parentid = 'ID1'
        LEFT JOIN mytable2 AS b ON b.id = 'ID2'

答案 1 :(得分:1)

您的查询很好(尽管我将使用from tkinter import * CURRENT_CANVAS_WIDTH = 500 CURRENT_CANVAS_HEIGHT = 500 CANVAS_WIDTH_SCREEN = 500 CANVAS_HEIGHT_SCREEN = 500 CANVAS_WIDTH = 6000 CANVAS_HEIGHT = 1000 FULL_SCREEN_STATUS = False def create_point(event): x1 = event.x y1 = event.y x2 = x1 + (CURRENT_CANVAS_WIDTH - CANVAS_WIDTH_SCREEN) y2 = y1 + (CURRENT_CANVAS_HEIGHT - CANVAS_HEIGHT_SCREEN) print(x2, y2) canvas.create_oval(x2, y2, x2 + 3, y2 + 3, fill="red") def scroll_start(event): canvas.scan_mark(event.x, event.y) def scroll_move(event): canvas.scan_dragto(event.x, event.y, gain=1) def screen_move_to_coords(x, y): global CURRENT_CANVAS_WIDTH, CURRENT_CANVAS_HEIGHT if not FULL_SCREEN_STATUS: end_x = CURRENT_CANVAS_WIDTH firs_x = (CURRENT_CANVAS_WIDTH - CANVAS_WIDTH_SCREEN) half_x_screen = (end_x + firs_x)//2 scrolls_x = x - (half_x_screen) print(scrolls_x) canvas.xview_scroll(scrolls_x, UNITS) end_y = CURRENT_CANVAS_HEIGHT firs_y = (CURRENT_CANVAS_HEIGHT - CANVAS_HEIGHT_SCREEN) half_y_screen = (end_y + firs_y)//2 scrolls_y = y - (half_y_screen) canvas.yview_scroll(scrolls_y, UNITS) CURRENT_CANVAS_WIDTH += scrolls_x CURRENT_CANVAS_HEIGHT += scrolls_y def resize_screen(event): global FULL_SCREEN_STATUS, CAN_RESIZE, CURRENT_CANVAS_WIDTH, CURRENT_CANVAS_HEIGHT, CANVAS_WIDTH_SCREEN, CANVAS_HEIGHT_SCREEN if FULL_SCREEN_STATUS: FULL_SCREEN_STATUS = False CANVAS_WIDTH_SCREEN = KEEP_CANVAS_WIDTH_SCREEN CANVAS_HEIGHT_SCREEN = KEEP_CANVAS_HEIGHT_SCREEN master.geometry('%dx%d+%d+%d' % (CANVAS_WIDTH_SCREEN + 4, CANVAS_HEIGHT_SCREEN + 4, xs, ys)) canvas.config(width=CANVAS_WIDTH_SCREEN, height=CANVAS_HEIGHT_SCREEN) print(CURRENT_CANVAS_WIDTH, CURRENT_CANVAS_HEIGHT) elif not FULL_SCREEN_STATUS: FULL_SCREEN_STATUS = True master.geometry('%dx%d+%d+%d' % (ws + 10, hs + 4, -10, -33)) canvas.config(width=ws, height=hs) CURRENT_CANVAS_WIDTH = CURRENT_CANVAS_WIDTH + (ws - CANVAS_WIDTH_SCREEN) CURRENT_CANVAS_HEIGHT = CURRENT_CANVAS_HEIGHT + (hs - CANVAS_HEIGHT_SCREEN) CANVAS_WIDTH_SCREEN = ws CANVAS_HEIGHT_SCREEN = hs print(CURRENT_CANVAS_WIDTH, CURRENT_CANVAS_HEIGHT) master = Tk() ws = master.winfo_screenwidth() hs = master.winfo_screenheight() xs = (ws / 2) - (CANVAS_WIDTH_SCREEN / 2) ys = (hs / 2) - (CANVAS_WIDTH_SCREEN / 2) master.geometry('%dx%d+%d+%d' % (CANVAS_WIDTH_SCREEN + 4, CANVAS_HEIGHT_SCREEN + 4, xs, ys)) master.title("...") master.resizable(False, False) canvas = Canvas(master, width=CANVAS_WIDTH_SCREEN, height=CANVAS_HEIGHT_SCREEN, bg="white") canvas.configure(scrollregion=(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT), yscrollincrement='1', xscrollincrement='1') canvas.focus('character') canvas.grid(row=0) canvas.bind("<Button-3>", create_point) canvas.bind("<ButtonPress-1>", scroll_start) canvas.bind("<B1-Motion>", scroll_move) canvas.bind("<Button-2>", lambda event, a=500, b=500: screen_move_to_coords(a, b)) master.bind("<Escape>", resize_screen) master.mainloop() 。但是,我希望使用具有特定值的行。我可以这样表述:

CROSS JOIN

您可能需要SELECT (CASE WHEN EXISTS (SELECT 1 FROM mytable1 WHERE parentid = 'ID1' AND flag = 'Y') AND EXISTS (SELECT 1 FROM mytable2 WHERE id = 'ID2' AND flag = 'Y') THEN 1 ELSE 0 END) as flag ,具体取决于您的数据库。

答案 2 :(得分:0)

最好使用JOIN而不是执行子查询

SELECT mytable1.parentid, mytable2.id 
FROM mytable1
JOIN mytable2 ON mytable2.flag = "Y" AND mytable1.flag = "Y"