MySQL - 显示记录及其父项

时间:2015-04-25 13:55:29

标签: mysql sql database

如何显示特定记录及其父母(最多两个)?

    import Cocoa

    class HeartView: NSView {

        var mouseLocation : NSPoint = NSZeroPoint

        func drawObject(){
            //Create an empty Bezier path
            let aBezier : NSBezierPath = NSBezierPath()
            aBezier.moveToPoint(CGPoint(x: 176.95,y: 44.90))
            aBezier.curveToPoint(CGPoint(x: 166.71,y: 145.89),
                controlPoint1: CGPoint(x: 76.63,y: 76.78),
                controlPoint2: CGPoint(x: 82.59,y: 206.70))
            aBezier.curveToPoint(CGPoint(x: 176.95,y: 44.90),
                controlPoint1: CGPoint(x: 237.55,y: 224.76),
                controlPoint2: CGPoint(x: 276.83,y: 95.98))
            aBezier.closePath()
                    if (aBezier.containsPoint(NSMakePoint(mouseLocation.x, mouseLocation.y))){
                        NSColor.yellowColor().setFill()
                        NSColor.greenColor().setStroke()        
                    } else {
                        NSColor.blueColor().setFill()
                        NSColor.orangeColor().setStroke()
                    }
                    aBezier.fill()
                    aBezier.lineWidth = 2.0
                    aBezier.stroke()
         }

         override func drawRect(dirtyRect: NSRect) {
            super.drawRect(dirtyRect)
            drawObject()
        }

        override func mouseDown(theEvent: NSEvent) {
            mouseLocation.x = theEvent.locationInWindow.x
            mouseLocation.y = theEvent.locationInWindow.y
            self.setNeedsDisplayInRect(self.frame)  
        }

我不知道怎么做。以下查询不起作用。

Table:
+---------------+
| id | parentID |
+---------------+
|  1 |     null |
|  2 |        1 |
|  3 |        1 |
|  4 |        2 |
|  5 |        2 |
|  6 |        2 |
+---------------+

The expected result:
Showing record with id 4 and his two parents

+---------------+
| id | parentID |
+---------------+
|  1 |     null |
|  2 |        1 |
|  4 |        2 |
+---------------+

我对高级查询的了解很差。请帮助:)

1 个答案:

答案 0 :(得分:0)

你需要CURSOR和LOOP。你将从孩子一直循环到根(直到parentId为NULL)。

http://www.mysqltutorial.org/mysql-cursor/