只有在表mysql pdo php中存在id时才从表中选择

时间:2015-06-20 05:10:06

标签: php mysql join pdo

我想在3个或更多表上使用join创建一个select查询。我有3个表,即t1t2t3和所有3个表中都存在的公共列id。如果表中存在id,我想选择3表,我的查询是这样的。

Select * from t1 
inner join t2 on t1.id = t2.id 
inner join t3 on t2.id = t3.id 
where t1.id = 1 and t2.id = 1 and t3.id = 1

如果所有3个表中都存在id,则查询返回值。但如果它不在任何表格示例t3中,我将不会返回任何内容。我正在寻找一种方法,如果它t3中不存在,我应该继续选择t1t2

3 个答案:

答案 0 :(得分:0)

我认为这些sql会对你有所帮助

public class Location extends SherlockFragment implements   View.OnClickListener {

    GoogleMap googleMap;
    Fragment fragment;
    Button arrived_mbtn;
    TextView current_mtv,request_mtv;
    LinearLayout btn_mlayout,journey_mlayout;
    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView  = inflater.inflate(R.layout.fragment_location, container, false);
        fragment = getFragmentManager().findFragmentById(R.id.map);
        googleMap = ((SupportMapFragment)fragment).getMap();
        googleMap.setMyLocationEnabled(true);
        googleMap.addMarker(new MarkerOptions().position(new LatLng(41.009471, 28.916134)).title("baslik"));
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(41.009471, 28.916134), 12.0f));
        btn_mlayout = (LinearLayout)rootView.findViewById(R.id.layoutbtn);
        journey_mlayout = (LinearLayout)rootView.findViewById(R.id.layoutjourney);


        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("you are in oncreate", "dfsdfsd");
        setHasOptionsMenu(true);

    }

    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub



        try {

            if (fragment != null) {

                fragment = getFragmentManager().findFragmentById(R.id.map);

                FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
                ft.remove(fragment);
                ft.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Thread.interrupted();
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tvrequest:
               /* btn_mlayout.setVisibility(View.GONE);
                arrived_mbtn.setVisibility(View.GONE);
                journey_mlayout.setVisibility(View.VISIBLE);
                getActivity().setTitle("Start a Ride");*/

            /*   Fragment fragment = new Driver_Maps_View();///change your frament name with history only..
               FragmentManager fragmentManager = getFragmentManager();
               fragmentManager.beginTransaction()                     .replace(R.id.content_frame, fragment).addToBackStack(null).commit();*/
                break;
        }
    }
}

这些sql对你也很有用

var result = (from t1 in dbContext.tblWISTransacs
              join t2 in dbContext.tblWCBTransacs on 1 equals 1
              where (t1.TicketNo == t2.TicketNo || t1.TicketNo == t2.customernumber)
              select new { t1, t2 }).ToList();

谢谢。

答案 1 :(得分:0)

这是你需要的吗?

SELECT * 
FROM t1 
INNER JOIN t2 on t1.id = t2.id 
LEFT JOIN  t3 on t2.id = t3.id 
WHERE t1.id = 1 AND t2.id = 1 AND (t3.id = NULL OR t3.id = 1)

答案 2 :(得分:0)

SELECT * FROM t1 JOIN t2 ON t1.id = t2.id 
LEFT JOIN t2 ON t1.id= t2.id WHERE t1.id= 1

如果t3

中不存在id,则t3列将返回null