I use of codeigniter(php) and have a table in database i want select query equal words no whatever it looks like it is.
For example I have the following table in the database MySQL:
I want get row that have num 2 of Active column.what do i do?
+----------------------------------------+
| Name Active Insert_Time |
+----------------------------------------+
| mon 2222 1481203712 |
| tue 2 1481202788 |
| wed 222 1481202581 |
| thu 22 1479902588 |
+----------------------------------------+
I've tried like this:
$search_term = $this->input->post('search');
$this->db->select('Active, Name', FALSE);
$this->db->where('Active LIKE', '%'.$search_term_num.'%');
$query = $this->db->get('submit_house');
this query give me all rows, but I'd just row 2
Use $this->db->get_where():
$query = $this->db->get_where('database_table', array('Active' => 2));
Source:
https://www.codeigniter.com/userguide3/database/query_builder.html
Related
I have a table prices with where I store more times in a day more values referred to a customer like this:
Table prices:
| id | customer_id | value_1 | value_2 | value_3 | created_at |
-------------------------------------------------------------------------
| 1 | 10 | 12345 | 122 | 10 | 2021-08-11 10:12:40 |
| 2 | 10 | 22222 | 222 | 22 | 2021-08-11 23:56:20 |
| 3 | 12 | 44444 | 444 | 44 | 2021-08-12 08:12:10 |
| 4 | 10 | 55555 | 555 | 55 | 2021-08-13 14:11:20 |
| 5 | 10 | 66666 | 666 | 66 | 2021-08-13 15:15:30 |
| 6 | 10 | 77777 | 777 | 77 | 2021-08-13 16:12:50 |
I have some filters on that table to retrieve only records with date greater than X and/or lower than Y, sort records by value_1 or value_2, etc...
With that filters I have to take only 1 record for each day of a customer specified.
I'm able to get the record with the highest value_1 for example, by using sql function max() and group by date.
// Init query
$query = Price::query();
// Take the greatest value of value1
$query = $query->selectRaw(
'max(value_1) as highest_value_1, ' .
'date(created_at) as date'
);
// If defined, add a greater or equals
if ($from) $query->where("created_at", ">=", $from);
// If defined add a lower or equals
if ($to) $query->where("created_at", "<=", $to);
// Get results for current customer only, grupping by date and ordering it
$query = $query->where('customer_id', $id)->groupBy('date')
->orderBy('date', 'DESC');
// Fetch records
$records = $query->get();
But now I would like to have only the last record for each day of a customer specified.
I need an eloquent/sql solution because the date range to search may be large and the table has a lot of records.
How can I archive that?
Thanks
Not a complete solution (no customer filter nor laravel use), but I would use something like that in pure sql :
SELECT
CONCAT(EXTRACT(YEAR_MONTH FROM created_at),EXTRACT(DAY FROM created_at)) AS day,
MAX(created_at)
FROM mytable
GROUP BY day;
Of course, you may use other function to group by day (like string regexp or substr).
i have a table like :
.............................
id | Sun | Mon | Tue | Wed |
.............................
1 | 0 | 1 | 1 | 1 |
.............................
Now i am getting all the column name from this query:
DB::getSchemaBuilder()->getColumnListing('weak');
it returning me the array of all columns name
but i want those column name which have the 1 value how can i get those column which have 1 value in laravel..any anyone please help me related this ??
You can use array_keys() with the second parameter:
$object = DB::table('weak')->first();
$columns = array_keys((array)$object, 1);
The result will be:
['Mon', 'Tue', 'Wed']
I think you need to build a Custom query using the IF function
SELECT
(IF(table.Sun == 1) table.Sun,)
(IF(table.Mon == 1) table.Mon,)
......
table.id
FROM table
I have 3 data with the same name fields but with different values
id | name | date
------ | ------ | -----------
1 | john | 2015-10-02
2 | john | 2016-09-10
3 | john | 2017-08-10
I want to combine into a single three value fields by name, and its value appears by fields year 2015, year 2016, year 2017. like the example below
id | name | 2015 | 2016 | 2017
------ | ------ | ----------- | ------------ | -----------
1 | john | 2015-10-02 | 2016-09-10 | 2017-08-10
I tried using group_by but only 1 field values that appear.
This my models.
function viewkal() {
$this->db->order_by('name','ASC');
$this->db->group_by('name');
$query = $this->db->get('mytabel');
return $query->result();
}
You should use an aggegation function like max or min for grouped by function
$this->db->select_max('date 1');
$this->db->select_max('date 2');
$this->db->select_max('date 3');
You can use a custom SQL query for this. Something like this will group concatente the fields:
SELECT name,
GROUP_CONCAT(date1 SEPARATOR '') AS date1,
GROUP_CONCAT(date2 SEPARATOR '') AS date2,
GROUP_CONCAT(date3 SEPARATOR '') AS date3
FROM `mytable` GROUP BY name
We define the separator to be none, else we will have commas around the fields. This method works for string and date fields.To use a custom query in CodeIgniter pass the query to:
$this->db->query("YOUR QUERY");
I have a table entries similar as follows:
+---------+---------+----------+
| Test_id | User_id | Attempts |
+---------+---------+----------+
| 12 | 5 | 1 |
| 13 | 5 | 1 |
| 12 | 5 | 2 |
+---------+---------+----------+
Now I want to select the elements group by test_id and should get the latest entry.
I tried this query:
$tests_took = Testresult::where('course_id', $courseId)
->where('user_id', Auth::id())
->groupby('test_id')
->orderBy('attempts', 'desc')
->get();
When I display the result, I'm getting the first two rows only (only one row for one test_id - which I what I want.) But instead of the last row for the test_id=12, it shows the first row. I always want the biggest attempt to be displayed.
My current output is like:
| 12 | 5 | 1 |
| 13 | 5 | 1 |
But I want this:
| 12 | 5 | 2 |
| 13 | 5 | 1 |
How can I achieve this? to get the latest row as the first array element when I use groupby or is there any other way to do this?
ORDER BY and GROUP BY don't work very well together...
If you simply want the highest attempt per test_id i suggest using the MAX() function:
$tests_took = Testresult::select('test_id', 'user_id', DB::raw('MAX(attempts) AS max_attempts'))
->where('course_id', $courseId)
->where('user_id', Auth::id())
->groupby('test_id')
->get();
You may use the following lines:
Testresult::select('*')
->join(DB::raw('(Select max(id) as last_id from Testresult group by test_id) LatestId'), function($join) {
$join->on('Testresult.id', '=', 'LatestId.last_id');
})
->orderBy('attempts', 'desc');
}
I need to write a query to retrieve values from two columns using mysql table
My table has the following strucutre
| ID | user_id | datetime | message |
| 1 | 21 | 2012-05-10 04:13:01 | message1 |
| 2 | 07 | 2012-05-10 04:17:51 | message2 |
| 3 | 21 | 2012-05-11 04:21:51 | message3 |
| 4 | 21 | 2012-05-11 04:43:51 | message4 |
| 5 | 07 | 2012-05-11 04:21:51 | message5 |
| 5 | 21 | 2012-05-11 04:43:51 | message6 |
i wrote the below query
$query="SELECT MAX(datetime) FROM messages where user_id=21 and date=2012-05-11";
but i am not getting latest record from table iam getting null value
help us
$query="SELECT MAX(datetime) FROM messages where user_id=21 and date LIKE '2012-05-11%'";
You should use DATE(date) to get date of timestamp. MySQL function DATE() extracts only date without hours, minutes and seconds.
Here is your query:
SELECT MAX(datetime)
FROM messages
WHERE user_id = 21 AND DATE(date) = '2012-05-11'
Have you tried the following?
$query="SELECT MAX(datetime) FROM messages where user_id=21";
Update:
In your question, you didn't specify if you wanted to retrieve last record for a certain date. If you do, you'd need to use MySQL's date function.
Are you looking for the most recent record? You can get this with a subquery:
$query = "SELECT * FROM messages WHERE datetime = (SELECT MAX(datetime) FROM messages)";
Your query asks for ".....date=2012-05-11";" but your table does not have the field named date? did you mean datetime? if so, you may want to try ".....datetime like '2012-05-11%'";