比较两个表并找到不匹配的记录

时间:2016-09-15 19:23:53

标签: php mysql

我想比较具有几乎desame列名的两个不同的表,并通过在具有相同值的列上标记1来输出它们,并且在ID列未被触及的情况下使用不同的值输出0。

表1

dirs=( /test/{a,b,c}/"$dirname" )
for dir in "${dirs[@]}"; do
    if [[ ! -d $dir ]]; then
         mkdir "$dir" 
         echo "Created Directory"
    else
         echo >&2 "$dir already exists"
    fi
done

表2

private void getsearch(string serchFor)
        {
            YouTubeRequestSettings setting = new YouTubeRequestSettings("youtubesearch", "APIkey","ise34857@gmail.com","password for my email");
            YouTubeRequest Request = new YouTubeRequest(setting);
            YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);
            query.OrderBy = "viewCount";
            query.Query = serchFor;
            query.SafeSearch = YouTubeQuery.SafeSearchValues.Moderate;
            Feed<Video> videofeed = Request.Get<Video>(query);
            videoLookUp = new Dictionary<string, string>();
            foreach (Video v in videofeed.Entries)
            {
                if (v.Media.Content!=null)
                {
                    Console.WriteLine(v.Title);
                    Console.WriteLine(v.Media.Content.Url);
                    comboBox1.Items.Add(v.Title);
                    videoLookUp.Add(v.Title,v.Media.Content.Url);
                }
            }
 }

输出

ID | Value | Color
1  | good  | red
2  | bad   | blue

其中,0是具有不同列值的值。

1 个答案:

答案 0 :(得分:0)

SELECT table1.id, 
  CASE WHEN table1.value=table2.value THEN 1 ELSE 0 END AS value,
  CASE WHEN table1.color=table2.color THEN 1 ELSE 0 END AS color
FROM table1 JOIN table2 ON table2.id=table1.id
ORDER BY 1;

或更短的方法:

SELECT table1.id, (table1.value=table2.value) AS value, (table1.color=table2.color) AS color
FROM table1 JOIN table2 ON table2.id=table1.id
ORDER BY 1;

MySQL语法的方法:

SELECT table1.id,
  IF(table1.value=table2.value, 1, 0) AS value,
  IF(table1.color=table2.color, 1, 0) AS color
FROM table1 JOIN table2 ON table2.id=table1.id
ORDER BY 1;