I am running a query to populate a dropdown menu, however the column I am using a list of company names, this means that the company name is often repeated, is there way to only get each repeated value only once? So for instance if I have in the table something like,
Company 1
Company 1
Company 2
Company 3
Company 4
Company 4
But I would like the dropdown to return,
Company 1
Company 2
Company 3
Company 4
I am using the database libray and active record to write the sql like this currently I need to know what I use to only show each result once,
function getAllUsers() {
$this->db->select('*');
$this->db->from('userProfileTable');
$query = $this->db->get();
return $query->result_array();
}
Or what would the raw sql be?
Use:
$this->db->distinct();
$this->db->select('*');
$this->db->from('userProfileTable');
$query = $this->db->get();
return $query->result_array();
You can use $this->db->distinct(); which adds the DISTINCT keyword to your query.
Modify your function to this:
function getAllUsers() {
$this->db->distinct();
$query = $this->db->get('userProfileTable');
return $query->result_array()
}
which produces
SELECT DISTINCT * FROM userProfileTable
Please note that select and from have been removed from your original function because get simply does the same thing.
Use DISTINCT keyword:
SELECT DISTINCT * from userProfileTable;
This should do the trick:
function getAllUsers() {
$this->db->distinct();
$query = $this->db->get('userProfileTable');
return $query->result_array();
}
Related
I am trying to fetch data from 2 tables with a join query. Here I have 2 columns in 2 tables with same column name.
This is my query:
public function get_all_expenses()
{
$this->db->select("*",'category.name as cat_name');
$this->db->from('expense');
$this->db->join('category','expense.cat_id = category.id');
$this->db->join('users','expense.user_id = users.id');
$query = $this->db->get();
return $query;
}
I can fetch data of 1 column of 1 table. But I can't fetch data of another column of another table. I am using CodeIgniter.
According to the CodeIgniter documentation the database select method accept a single argument. The correct syntax for the select is then:
$this->db->select('*, category.name as cat_name');
So my question is:
Is it possible to select all data from different tables in one query?
Example1:
$query = $this->db->get('table1');
$query = $this->db->get('table2');
$query = $this->db->get('table3');
return $query->result();
Example2:
$this->db->select('*');
$this->db->from('table1', 'table2', 'table3');
$query = $this->db->get();
return $query->result();
I think the second exaple is possible. If not i want to ask how you would do that.
Thank you.
It can be done by putting the table names in an arrary
$query = $this->db
->select('*')
->from(['table1', 'table2'])
->get();
return $query->result();
But the number of rows in the result will be the product of the number of rows in each table, i.e. table1 has 3 rows and table2 has 19 you'll get 57 rows in the result. Are you sure that's what you want?
Joins are easy to write and highly efficient. Don't be afraid of them.
select distinct clientID from Client where clientID not in (select clientID from courseDetails inner join course on coursedetails.courseID = course.courseID where coursedetails.courseID = '$courseID')
If your query is a complex one then you can use RAW query in laravel like:
$data = DB::select(DB::raw('your query'));
Reference
Note: DB::raw() is used to make arbitrary SQL commands which aren't parsed any further by the query builder. They therefore can create a vector for attack via SQL injection.
I give you a starting point:
$results = DB::table('Client')
->whereNotIn('clientID', function($query) use ($courseID) {
$query->select('clientID')
->from('courseDetails')
->join('course', 'courseDetails.courseID', '=', 'course.courseID')
->where('coursedetails.courseID', '=', $courseID);
})->get();
This should get you going. You can tweak it as you want to get your expected result.
Adding to #Mayank answer, you can run raw SQL and pass parameter like this
$result = DB::select('select distinct... where coursedetails.courseID = ? ', [$courseID]);
I have a candidate and a status table which every candidate have multiple statuses. when statuses changes new row in statuses table will be created to store new status. So when user wants to filter candidates by status I want to select candidates that their current status is what user select not the all before statuses.
my query is :
$status = $request->get('status');
$q->where(function ($q) use ($status) {
$q->orWhereHas('statuses', function ($q) use ($status) {
$q->where('status', 'like', "%" . $status . "%");
});
});
I changed the query to the blow and it's working fine.
$q->whereRaw('status LIKE "%'.$status.'%" AND id=(select max(id) from statuses WHERE status LIKE "%'.$status.'%")');
EDIT:
The query in above has a problem and that is it's only get's first matching candidate not all of them so I changed the query to this:
$q->where(function ($q) use ($status) {
$q->orWhereHas('statuses', function ($q) use ($status) {
$q->whereRaw('status LIKE ? AND id IN (SELECT MAX(id) FROM statuses GROUP BY candidate_id)',["%".$status."%"]);
});
});
my percipience is, current status of a candidate is last record.
i say mysql direction, as for your migrate and modeling, implement this.
select * from status
where id =(
select max(id) from status
where candidate_id= x
)
x is candidate id.
I need to filter duplicates, but union is displaying duplicates.
For example Query-1 is displaying 5 tuples like 1,2,3,4,5.
Query-2 generating 3 tuples like 1,2,6.
Union of both the tuples displaying result 1,2,3,4,5,1,2,6.
But I want the result as 1,2,3,4,5,6.
Here is my controller :
public function product()
{
$product = $this->input->post('keyword');
$temp = explode(" ", $product);
$count = count($temp);
for($i=0;$i<$count;$i++)
{
$query = "SELECT * FROM `product` WHERE SOUNDEX(`name`) LIKE CONCAT('%',SOUNDEX('$temp[$i]'),'%') UNION SELECT * FROM `product` WHERE `name` like '%$temp[$i]%'";
$data = $this->Back_model->getby_query($query);
$records = json_encode($data);
echo $records;
}
}
There is no apparent reason for using 2 select queries, the second filter may be added to an existing where clause
SELECT *
FROM `product`
WHERE SOUNDEX(`name`) LIKE CONCAT('%',SOUNDEX('$temp[$i]'),'%')
AND `name` like '%$temp[$i]%'";
First of all, I would probably do this in another scripting language, but if you really want to do this in MySQL, you need to use DISTINCT. You should query the data and put it in a temp table, and from the temp table query the DISTINCT values. Once you're done you can drop the temp table. For an operation this small, not sure if this is what I would do, but if you say that you have thousands of records/values that need to be filtered, than it might be worth it.