MySQL连接多个表的列

时间:2017-03-03 10:19:45

标签: mysql

我有3张桌子。如何根据这种情况显示cardNumber和相应的组。然后我可以创建一个视图并查看卡号以及它所属的组。

 **1.ClientCards:**
 1.1cardID
 1.2cardNumber
 1.2relCardTypeID - foreign key of 2.1cardTypeID

 **2.cardTypes:**
 2.1cardTypeID - foreign key of 3.1groupID
 2.2relParrentID
 2.2cardTypeName

 **3.cardGroups:**
 3.1groupID
 3.1groupName

2 个答案:

答案 0 :(得分:0)

希望您的映射错误,因为relParrentID应该是3.1 groupID的外键

试试这个:

#include <vector>
#include <stdlib.h>
#include <cstdlib>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;

class SimpleImageEffect
{
public:
    virtual void processImage(vector<Point> &points) = 0;
};

class RemoveRed : public SimpleImageEffect
{
public:

    virtual void processImage(vector<Point> &points)
    {
        for (Point& p : points)
        {
            p.setRed(0);
        }
    }
};
//Just an example of one of the effect classes. 
//The code in them is correct, so I won't include the others unless needed.

vector<Point> parse_line(string line)
{
    istringstream scanner{line};
    vector<Point> result{};
    int red = -1;
    int green = -1;
    int blue = -1;
    int counter = 0;

    while(scanner.good())
    {
        if (counter == 0)
        {
            counter++;
            scanner >> red;
        }
        else if (counter == 1)
        {
            counter++;
            scanner >> green;
        }
        else if (counter == 2)
        {
            scanner >> blue;
            Point p{ red, green, blue };
            result.push_back(p);
            counter = 0;
        }
    }
    return result;
}

void readFromCin()
{
    string line = "";

    vector<string> lines_in_file{};
    int i, effect_choice;
    SimpleImageEffect *effect = nullptr;

    getline(cin, line);

    while (line.length() > 0)
    {
        lines_in_file.push_back(line);
        getline(cin, line);
    }

    for (int i = 0; i < lines_in_file.size(); i++)
    {
        if (lines_in_file[i] != "P3")
        {
            effect_choice = strToInt(lines_in_file[i]);
        }

        else if (lines_in_file[i] == "P3")
        {
            cout << lines_in_file[i] << endl;
            cout << lines_in_file[i+1] << endl;
            cout << lines_in_file[i+2] << endl;
        }

        vector<Point> points = parse_line(lines_in_file[i]);

        if (effect_choice == 1) effect = new RemoveRed;
        if (effect_choice == 2) effect = new RemoveGreen;
        if (effect_choice == 3) effect = new RemoveBlue;
        if (effect_choice == 4) effect = new NegateRed;
        if (effect_choice == 5) effect = new NegateGreen;
        if (effect_choice == 6) effect = new NegateBlue;
        if (effect_choice == 7) effect = new AddNoise;
        if (effect_choice == 8) effect = new HighContrast;
        if (effect_choice == 9) effect = new ConvertToGrayscale;

        effect->processImage(points);

        for (auto p : points)
        {
            cout << p;
            cout << endl;
        }
    }
}

int main(int argc, char** argv)
{
    string menu_choice;
    getline(cin, menu_choice);
    if (menu_choice == "1")
    {
        readFromFile();
    }
    else
    {
        readFromCin();
    }
    return 0;
}

答案 1 :(得分:0)

VIEW `card_groups` AS
SELECT 
    `c`.`cardNumber` AS `cardNumber`,
    `g`.`groupName` AS `groupName`,
    `t`.`cardTypeName` AS `cardTypeName`
FROM
    ((`s2_1_cards` `c`
    LEFT JOIN `fxml4_4_card_types` `t` ON ((`c`.`relCardTypeID` = `t`.`cardTypeID`)))
    LEFT JOIN `fxml4_3_groups` `g` ON ((`g`.`groupID` = `t`.`relParrentID`)))