在MySQL中使用IF语句包含所有结果

时间:2018-10-07 20:45:04

标签: mysql mysqli

在我的SQL语句中,我要查询2个表以获取导航栏的标签和链接。该查询检查site_menu中的labelpage_id,然后从filename表中获取site_pages

我遇到的问题是可能存在下拉菜单,因此下拉菜单的label将不具有关联的page_id,但我仍然希望在结果中返回它

我相信可以将If / ELSE功能添加到SQL语句中?

     SELECT
      site_menu.page_id,
      site_menu.label,
      site_pages.filename
     FROM
      site_menu,
      site_pages
     WHERE
      site_menu.site_id = 1
     AND
      site_menu.parent_id = 0
     AND
      site_pages.id = site_menu.page_id
     ORDER BY
      site_menu.ord
     ASC

1 个答案:

答案 0 :(得分:0)

您应将JOIN隐式,LEFT JOIN)替换为site_pages,以便无论 SELECT site_menu.page_id, site_menu.label, site_pages.filename FROM site_menu LEFT JOIN site_pages ON site_pages.id = site_menu.page_id WHERE site_menu.site_id = 1 AND site_menu.parent_id = 0 ORDER BY site_menu.ord ASC 表中是否存在条目,查询都将返回结果:

import React from 'react';
import { StyleSheet, TextInput, View } from 'react-native';

export default class App extends React.Component {

  state={
    message:"Hi {name}, your rating for this app is {rating}"
  }

    handleName=()=>{
      this.setState({
        message:"Hi "+this.refs.name.value+", your rating for this app is {rating}"
      })
    }

    handleRating=()=>{
      this.setState({
        message:"Hi "+this.refs.name.value+", your rating for this app is "+this.refs.rating.value
      })
    }

  render() {
    return (
      <View style={styles.container}>
        <TextInput
          placeholder="Your Name"
          ref="name"
          onChangeText={this.handleName}
        />
        <TextInput
          placeholder="Your Rating"
          ref="rating"
          onChangeText={this.handleRating}
        />
        <View>{this.state.message}</View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});