Hi I want to select a row with max ID(primary key) value having patient_id=xyz
following is my sql script
check="Select * from notes where id=max(ID) in (SELECT * FROM notes WHERE patient_id="+patientSoapBean.getPatientID()+")";
I am getting invalid use of group function error. can you point out the error in this.
You likely are trying to do this:
SELECT * FROM notes
WHERE patient_id = ?
ORDER BY id DESC
LIMIT 1
Related
I'm trying to run this query
$tableDate = DB::select('SELECT (DATE_FORMAT(created_at,"%m-%d-%Y")) as
dateTaken FROM `test_answers`WHERE user_id= '.$id.' GROUP BY (test_id)
');
but it is giving me this error
SQLSTATE[42000]: Syntax error or access violation: 1055 'mte30.test_answers.created_at' isn't in GROUP BY (SQL: SELECT (DATE_FORMAT(created_at,"%m-%d-%Y")) as dateTaken FROM test_answers WHERE user_id= 2 GROUP BY (test_id) )
I tried it on PHPMyAdmin and it worked.
Can someone tell me why it's not working using Eloquent?
Thank you
The reason this doesn't work with strict mode on is because you haven't specified in the query which created_at to use. If you're using GROUP BY and you are selecting a column that isn't grouped on or aggregated, you may not receive the correct data.
You have multiple rows in test_answers but MySQL doesn't know which row's created_at you care about. You could fix this by simply telling MySQL which created_at to return, something like MAX(created_at) to select the most recent, or MIN(created_at) to select the oldest.
I am trying to count distinct values after Grouping by id then inserting them into another table with the following query:
INSERT INTO table_aggregate
(id_aggregate, aggregate_column)
(SELECT id_detail, COUNT(DISTINCT(detail_column))
FROM table_detail
GROUP BY id_detail)
ON DUPLICATE KEY UPDATE
aggregate_column = COUNT(DISTINCT(detail_column));
When run I get the error:
ERROR 1111 (HY000): Invalid use of group function
If I run the SELECT statement portion of the query it works fine. Why is it throwing this error?
You can not use COUNT in the UPDATE part. Use VALUES(aggregate_column) instead:
INSERT INTO table_aggregate
(id_aggregate, aggregate_column)
(SELECT id_detail, COUNT(DISTINCT(detail_column))
FROM table_detail
GROUP BY id_detail)
ON DUPLICATE KEY UPDATE
aggregate_column = VALUES(aggregate_column);
http://rextester.com/KTEDM89215
You can put the aggregation inside a subquery. Then you can refer to the computed value in the update
INSERT INTO
table_aggregate (id_aggregate, aggregate_column)
SELECT
*
FROM
(
SELECT
id_detail,
COUNT(DISTINCT(detail_column)) AS count
FROM
table_detail
GROUP BY
id_detail
) AS aggr
ON DUPLICATE KEY
UPDATE
aggregate_column = aggr.count
I have an SQL field defined as set('nightlife', 'food', 'sports', 'culture', 'movies', 'general')
Now I want to run a query where I pass for example nightlife,food and I want the result to contain ALL records where the Category contains nightlife or food. So for example a record with nightlife,culture,sports should also be returned as it contains nightlife. I'm not sure how to run this. I tried using the IN keyword in the following way:
'SELECT ... FROM table WHERE '$category' IN Categories
however this isn't working.
UPDATE
Running following query as suggested in answer:
SELECT *
FROM images
WHERE id = '2650225'
AND WHERE FIND_IN_SET('sports', Categories ) >0
OR FIND_IN_SET('nightlife', Categories ) >0
ORDER BY delete_at ASC
LIMIT 10
OFFSET 0
receiving following error :
1064 - 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 'WHERE find_in_set('sports', Categories) > 0 or find_in_set('nightlife', Categori' at line 1
You can use find_in_set with or:
select *
from yourtable
where find_in_set('nightlife', categories) > 0 or
find_in_set('food', categories) > 0
SQL Fiddle Demo
Based on your edits, you can't have multiple where clauses. Also you need to use parentheses around the or criteria:
SELECT *
FROM images
WHERE id = '2650225' AND
(FIND_IN_SET('sports', Categories ) > 0 OR
FIND_IN_SET('nightlife', Categories ) >0)
ORDER BY delete_at ASC
LIMIT 10
OFFSET 0
I have a MySQL query that works on my current MySQL database. I've been forced to move over to oracle, so I'm trying to port all my stored procedures / programs to use the Oracle SQL Syntax. I'm having a lot of trouble on one particular query. Here is the MySQL query. It updates a table using a subquery.
update table1 alf
set nextcontractid =
(
select
contractid from table1copy alf2
where
alf2.assetid = alf.AssetID
and
alf2.lasttradedate > alf.LastTradeDate
order by lasttradedate asc limit 1
)
where complete = 0
In oracle, I can't use the the limit command, so I've looked for the workaround. Here is my oracle query. (which doesn't work.)
update table1 alf
set nextcontractid =
(select contractid from
(
SELECT contractid, rownum as row_number
FROM table1copy alf2
WHERE alf2.assetid = alf.assetid
AND alf2.lasttradedate > alf.lasttradedate
ORDER BY lasttradedate ASC
)
where row_number = 1)
where alf.complete = 0
I get the following error:
Error at Command Line:8 Column:29
Error report:
SQL Error: ORA-00904: "ALF"."LASTTRADEDATE": invalid identifier
00904. 00000 - "%s: invalid identifier"
line 8 is:
AND alf2.lasttradedate > alf.lasttradedate
Removing the update statement and putting in some dummy values into the subquery yields the correct results for the subquery:
(select contractid from
(
SELECT contractid, rownum as row_number
FROM asset_list_futures_copy alf2
WHERE alf2.assetid = 'GOLD'
AND alf2.lasttradedate > '20110101'
ORDER BY lasttradedate ASC
)
where row_number = 1)
Looking at the error, it looks like the second reference to alf isn't working. Any idea how I can change my query so that it works in oracle?
Seems the parser does not like that, despite the fact it is sintacticaly correct. Probably the two imbricated and ordered clause is blinding him somehow. I reproduced that.
You can use an analytical function:
update table1 alf
set nextcontractid =
(SELECT min(contractid) keep (dense_rank first order by lasttradedate asc)
FROM table1copy alf2
WHERE alf2.assetid = alf.assetid
AND alf2.lasttradedate > alf.lasttradedate
)
where alf.complete = 0
You can use WHERE rownum = 1, or rownum BETWEEN x AND y in cases that you want more results.
I put together a test table for a error I recently came across. It involves the use of LIMIT when attempting to delete a single record from a MySQL table.
The error I speak of is "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 'LIMIT 1' at line 1"
The table I put together is called test; it has 3 columns, id, name and created. I populated the table with several records and then attempted to delete one. Below is the statement I used to try and accomplish this.
DELETE t FROM test t WHERE t.name = 'foo' LIMIT 1
Without the use of LIMIT 1, the statement executes just fine, but of course I wouldn't be using LIMIT if there wasn't a need for it.
I'm fully aware that I can use another statement to accomplish this DELETE successfully. See below:
DELETE FROM test WHERE name = 'foo' LIMIT 1
However my question is centered on why the first statement isn't working with LIMIT.
So my question is, what I have done incorrectly with respect to the first statement to generate this error?
simply use
DELETE FROM test WHERE 1= 1 LIMIT 10
the delete query only allows for modifiers after the DELETE 'command' to tell the database what/how do handle things.
see this page
From the documentation:
You cannot use ORDER BY or LIMIT in a multiple-table DELETE.
DELETE t.* FROM test t WHERE t.name = 'foo' LIMIT 1
#Andre If I understood what you are asking, I think the only thing missing is the t.* before FROM.
Use row_count - your_desired_offset
So if we had 10 rows and want to offset 3
10 - 3 = 7
Now the query delete from table where this = that order asc limit 7 keeps the last 3, and order desc to keep the first 3:
$row_count - $offset = $limit
Delete from table where entry = criteria order by ts asc limit $limit
There is a workaround to solve this problem by using a derived table.
DELETE t1 FROM test t1 JOIN (SELECT t.id FROM test LIMIT 1) t2 ON t1.id = t2.id
Because the LIMIT is inside the derived table the join will match only 1 row and thus the query will delete only this row.
First I struggled a bit with a
DELETE FROM ... USING ... WHERE query,...
Since i wanted to test first
so i tried with SELECT FROM ... USING... WHERE ...
and this caused an error , ...
Then i wanted to reduce the number of deletions adding
LIMIT 10
which also produced an error
Then i removed the "LIMIT" and - hurray - it worked:
"1867 rows deleted. (Query took 1.3025 seconds.)"
The query was:
DELETE FROM tableX
USING tableX , tableX as Dup
WHERE NOT tableX .id = Dup.id
AND tableX .id > Dup.id
AND tableX .email= Dup.email
AND tableX .mobil = Dup.mobil
This worked.