CI Is there a way to query custom order_by - php

Is it possible to customize the order_by query in MySQL or in CI ? Such as I want my column to be ordered by ('1', '11', '4', '2', '21', '3', '5', '7') So if I query it as ASC the result will show in the order of my customized order.
If it is not possible, what is the best workaround to get these order ? Hoping for a simple solution just using the MySQL query.
All answers and suggestions are greatly welcomed. Thanks.

Try this one.
$this -> db -> order_by('FIELD ( table.id, 1, 11, 4,2,21,3,5,7 )');
link

Pure Mysql answer is yes you can order a field by a set list with the MYSQL FIELD() function
SELECT *
FROM mytable
WHERE id IN ('1', '11', '4', '2', '21', '3', '5', '7')
ORDER BY FIELD(id, '1', '11', '4', '2', '21', '3', '5', '7')

Related

Column count doesn't match value count at row 1 error message, php / sql [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have to make a webpage using PHP and MySQL workbench to collect form data and I keep getting this error:
INSERT INTO tools ( id, tool1, tool2, tool3, tool4, tool5, tool6, tool7, tool8, tool9, tool10) VALUES ( '0','1', '2', '3', '4,' '5', '6', '7', '8', '9', '10')
INSERT failed: INSERT INTO tools ( id, tool1, tool2, tool3, tool4, tool5, tool6, tool7, tool8, tool9, tool10) VALUES ( '0','1', '2', '3', '4,' '5', '6', '7', '8', '9', '10')
Column count doesn't match value count at row 1
This is what my code looks like:
$query = "INSERT INTO tools ( id, tool1, tool2, tool3, tool4, tool5, tool6, tool7, tool8, tool9, tool10) VALUES ( '0','1', '2', '3', '4,' '5', '6', '7', '8', '9', '10')";
and this is what it looks like in workbench:
INSERT INTO tools (id, tool1, tool2, tool3, tool4, tool5, tool6, tool7, tool8, tool9, tool10) VALUES ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
I've tried it with an without the id and the 0 value, and neither work. Does anyone know how to fix this?
Your code
INSERT INTO tools ( id, tool1, tool2, tool3, tool4, tool5, tool6, tool7, tool8, tool9, tool10)
VALUES ( '0','1', '2', '3', '4,' '5', '6', '7', '8', '9', '10')
Has an error exactly between 4 and 5 the apostrophes are misspaced
It must look like this
INSERT INTO tools ( id, tool1, tool2, tool3, tool4, tool5, tool6, tool7, tool8, tool9, tool10)
VALUES ( '0','1', '2', '3', '4', '5', '6', '7', '8', '9', '10')

How can i remove the oldest entries in my SQL result?

I want to get only the last reports, for all items but only the most recent report for each item_id.
Here is my current solution:
SELECT distinct * FROM t.reports ORDER BY created DESC LIMIT 100
My table consists of the following columns:
id | user | item_id | created
'2', '1', '2643', '2017-06-13 16:28:34'
'3', '1', '19333', '2017-06-13 19:26:56'
'4', '1', '19333', '2017-06-13 19:29:24'
'5', '1', '1319', '2017-06-13 19:29:56'
'6', '1', '1319', '2017-06-13 19:30:16'
'7', '1', '1319', '2017-06-13 19:30:17'
'8', '1', '1319', '2017-06-13 19:30:18'
'9', '1', '1319', '2017-06-13 19:30:25'
'10','1', '1319', '2017-06-13 19:31:51'
I want no duplicate item_ids AND only the most recent entry for that item.
BUT i also want ALL reports, but no duplicate item reports!
EXAMPLE:
i expect that when i execute my query, i only get row 2,4 and 10 returned.
Try this:
select a.id,a.user,a.item_id,a.created
from reports as a
where a.created=(select max(created)
from reports as b
where a.item_id=b.item_id)
Try This let me know if this works.
SELECT *
FROM t.reports
where item_id in (
select distinct item_id
from t.reports
)
ORDER BY updated DESC
LIMIT 100
Try with this I have tested it on my test database:
SELECT DISTINCT item_id, MAX(created), id, user
FROM `t.reports`
GROUP BY item_id
ORDER BY MAX(created) DESC
You can use group by :
SELECT * FROM t.reports WHERE created IN (SELECT max(created) FROM t.reports)
GROUP BY item_id
LIMIT 100

Mysql syntax error server version

Error
INSERT INTO PENILAIAN (ID,KURSUSID,QUEST1,QUEST2,QUEST3,QUEST4,QUEST5,QUEST6,QUEST7,QUEST8,QUEST9,QUEST10)VALUES ('951019105851', ''13'', '1', '2', '3', '4', '4', '5', '4', '5', '4', '5')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '13'', '1', '2', '3', '4', '4', '5', '4', '5', '4', '5')' at line 3
code
"INSERT INTO PENILAIAN (ID,KURSUSID,QUEST1,QUEST2,QUEST3,QUEST4,QUEST5,QUEST6,QUEST7,QUEST8,QUEST9,QUEST10)VALUES
('$login_sessionID',
'$kid','$QUEST1','$QUEST2','$QUEST3','$QUEST4','$QUEST5','$QUEST6','$QUEST7','$QUEST8','$QUEST9','$QUEST10')";
Error simply guides you
''13''
^ ^
there is additional single quote
keep only one ' pair for code
Use prepared statements
I think you have double quotes on ''13''.
olso i want to ask, does any of the values contain quotes? like this (I'am x)
if you use quotes you should use mysql_real_escape_string when you POST

I need to change a complex MYSQL query in to a view or views

I have a query that creates a rolling sum and subtracts that from a sum total. I have the query working in the SQL window, but I need some help changing it in to a view or table that I can call from a PHP so I can use the results in building a graph.
This query builds the basis for a burndown report used in project management so it is very handy.
When I try to create a view out it I get multiple errors with joins and variables not being allowed in a view. I am kind of a mysql newbie and I am beating my head on this one.
Here is the SQL query:
SELECT
taskid,
projectid,
esthours,
actualhours,
eview.SE As TotalHours,
(#EST:=#EST - esthours) as ESTI,
(eview.SE + #EST) as Estimated,
(#EST2:=#EST2 - actualhours) as AC,
(eview.SE + #EST2) as Actual
from
tbltasks,
eview
JOIN
(SELECT #EST:=0) EE
JOIN
(SELECT #EST2:=0) E2;
Here is a sample of the results.
taskid, projectid, esthours, actualhours, TotalHours, ESTI,Estimated, AC, Actual
'1021', '2', '4', '3', '20', '-4', '16', '-3', '17'
'1022', '2', '3', '3', '20', '-7', '13', '-6', '14'
'1023', '2', '2', '4', '20', '-9', '11', '-10', '10'

Why when caching queries are relationships not cached?

Throughout querying my database with the queries as the one below;
Model::with('first','second','third','fouth','fiveth')
->orderBy('title')
->remember(2,'domain.query')
->get()
The overall query gets cached etc, but why don't the relationships that I attach to the query. Would I have to do a join on the models etc to accomplish everything getting cached etc?
Queries made in Clockwork:
SELECT * FROM `first_table` WHERE `first_table`.`id` in ('2', '4', '3')
SELECT * FROM `second_table` WHERE `second_table`.`id` in ('2', '5', '1', '4')
SELECT * FROM `third_table` WHERE `third_table`.`id` in ('2', '5', '1', '4')
SELECT * FROM `fouth_table` WHERE `fouth_table`.`id` in ('2', '5', '1', '4')
SELECT * FROM `fiveth_table` WHERE `fiveth_table`.`id` in ('2', '5', '1', '4')
"Why" is somewhat of a broad question, it is a design decision made by the framework author. The way to get around it, though it's not very pretty code, is to use eager load constraints:
Model::with([
'first' => function($q) { $q->remember(...); },
'second' => ...
])->remember(...)->get();

Categories