Welcome Guest, Not a member yet? Register   Sign In
Another MySQL question
#1

[eluser]codex[/eluser]
Imagine a 'project'-table with, besides the projectdata itself, 3 fields:

- project_creator_id = the id of the person who created the project
- project_client_id = the id of the person for whom the project is done
- project_contractor_id = the id of the person who will realise the project

The project creator can either be the client or the contractor, so at least 2 id's will be the same. I want to show the projects that were NOT created by the user id (uid) in the current session (me), but where I am either the client or contractor.

So in short: how would you say 'give me the rows where either uid is project_client_id or project_contractor_id, but NOT where uid is project_creator_id'

This doesn't work:
Code:
function get_co_projects()
    {
        $this->db->select('*');
        $this->db->from('projects');
        $this->db->where('project_client_id', $this->session->userdata('uid'));
        $this->db->orwhere('project_contractor_id', $this->session->userdata('uid'));
        $this->db->where('project_creator_id !=', $this->session->userdata('uid'));

        $query = $this->db->get();

        return $query->result();
    }

It's basically the OR that screws it up.
#2

[eluser]Skulls[/eluser]
Code:
$try_this_sql=" SELECT * from projects WHERE ((project_client_id=$this->session->userdata('uid')) OR (project_contractor_id=$this->session->userdata('uid') ))
AND (project_creator_id !=$this->session->userdata('uid')) ";
$query=$this->db->query($try_this_sql);
$result=$query->result();


use the brackets to group conditions .. where (color=red or color=green) and (color!=black)
#3

[eluser]codex[/eluser]
[quote author="Skulls" date="1199140084"]
Code:
$try_this_sql=" SELECT * from projects WHERE ((project_client_id=$this->session->userdata('uid')) OR (project_contractor_id=$this->session->userdata('uid') ))
AND (project_creator_id !=$this->session->userdata('uid')) ";
$query=$this->db->query($try_this_sql);
$result=$query->result();


use the brackets to group conditions .. where (color=red or color=green) and (color!=black)[/quote]

Yaay! Didn't know about/think of bracketing, but it makes perfect sense. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB