I'm getting the callback error 'Incorrect parameters in the call to native function \'json_object\' for the below query in node.js but the same query works when I try it out in the mysql workbench?
SELECT t1.typeId AS typeId,
UNIX_TIMESTAMP(t1.hour) AS hour,
t2.userInfo AS userInfo
FROM UserCommits t1
INNER JOIN ( SELECT t1.id AS id,
JSON_OBJECT("id", t1.id,
"name", t1.name,
"birthUnix", UNIX_TIMESTAMP(t1.birthDate),
"gender", t1.gender,
"image", t2.secure_url,
"connectionInfo", t3.connectionInfo) AS userInfo
FROM Users t1
LEFT JOIN (
SELECT userId,
secure_url
FROM UserProfileImages
WHERE id IN (
SELECT MAX(id)
FROM UserProfileImages
GROUP BY userId
)
) t2
ON t1.id = t2.userId
LEFT JOIN (
SELECT connectionId,
JSON_OBJECT(
"id", id,
"createdUnix", UNIX_TIMESTAMP(createdDt)
) AS connectionInfo
FROM UserConnections
WHERE userId = 98
) t3
ON t1.id = t3.connectionId
) t2
ON t1.userId = t2.id
WHERE t1.locationId = 41
Found the issue..
To insert a JSON_Object from a subquery into another JSON_Object there mysql checks that there is a 1 to 1 relationship. I fixed the issue by updating the subquery to include a Group By statement on the join column.
(
SELECT connectionId,
JSON_OBJECT(
"id", id,
"createdUnix", UNIX_TIMESTAMP(createdDt)
) AS connectionInfo
FROM UserConnections
WHERE id IN (
SELECT MAX(id)
FROM UserConnections
WHERE userId = ${ownerId}
GROUP BY connectionId
)
)
Related
I got two table users(table 01)、record_dcm_upload(table02)
i try to query counts and latest upload file time by everylogin account(users.username)
like
SELECT record_dcm_upload.user_id, users.username, record_dcm_upload.upload_time, COUNT( * )
FROM record_dcm_upload
JOIN users ON ( users.id = record_dcm_upload.user_id )
GROUP BY record_dcm_upload.user_id
but my query sql got some problem (actually the result of upload_time not the latest)
how should i adjust my query code (hope user_id and upload_time all sort By DESC)
SELECT t2.id, t2.username, MAX(t1.upload_time), COUNT(*)
FROM record_dcm_upload t1
JOIN users t2 ON ( t2.id = t1.user_id )
GROUP BY t2.id, t2.username
I'm doing UNION in MySQL that I'm unable to troubleshoot for a while.
Error says that
syntax is incorrect around t1.*
Those 2 SELECTs work ok separately, checked. But UNION fails. I'm not custom to MySQL syntax, maybe something is wrong with that.
SELECT (
t1.*,
a.region_count
FROM
(
SELECT
data_region,
COUNT(*) AS region_count
FROM
t2
GROUP BY
data_region
) AS a
LEFT OUTER JOIN
t1
ON
t1.values_att0 = a.data_region
WHERE
t1.name_0 = 'region'
) AS b
UNION
SELECT (
t1.*,
c.age_gen_count
FROM
(
SELECT
data_dage,
data_gen,
COUNT(*) AS age_gen_count
FROM
t2
GROUP BY
data_dage,
data_gen
) AS c
LEFT JOIN
t1
ON
t1.values_att0 = c.data_dage AND
t1.id_question_1 = c.data_gen
WHERE
t1.name_0 = 'age' AND
t1.q_name_1 = 'gen'
)
You are using parenthesis around your SELECT field, this is your syntax error origin (the UNION is not the cause). Just remove them:
SELECT
t1.*,
a.region_count
FROM
(
SELECT
data_region,
COUNT(*) AS region_count
FROM t2
GROUP BY data_region
) AS a
LEFT OUTER JOIN t1
ON t1.values_att0 = a.data_region
WHERE t1.name_0 = 'region'
UNION ALL
SELECT
t1.*,
c.age_gen_count
FROM
(
SELECT
data_dage,
data_gen,
COUNT(*) AS age_gen_count
FROM t2
GROUP BY data_dage, data_gen
) AS c
LEFT JOIN t1
ON t1.values_att0 = c.data_dage
AND t1.id_question_1 = c.data_gen
WHERE t1.name_0 = 'age'
AND t1.q_name_1 = 'gen'
My Table Details image with test data
I have come up with below code:
select tableName,userDets,regDate
from (
(SELECT 'Table1' AS tableName, t1.username as userDets, t1.created_date as regDate FROM table1 t1 group by username )
union all
(SELECT 'Table2' AS tableName, t2.username as userDets, t2.created_date as regDate FROM table2 t2 group by username )
union all
(SELECT 'Table3' AS tableName, t3.username as userDets, t3.created_date as regDate FROM table3 t3 group by username )
) AS allTableCombo
group by allTableCombo.userDets
If in this code i use order by date at last and then make it subquery for another select with group by username , I can obtain required output. But I dont want to use sbuquery.
Please if anyone could provide me with an alternative.. I tried with self join but was not success on using it....
I have a database of magento with double images, I want to delete those but first i got to detect them with a sql query.
I have tried this code
select t1.VALUE from catalog_product_entity_media_gallery t1
join catalog_product_entity_media_gallery t2 on (t1.value = t2.value)
this one:
select * from catalog_product_entity_media_gallery where value=value
and this one:
select
*
from
(
select
value
from
catalog_product_entity_media_gallery
group by
value
having count(*) > 1
) as t
inner join catalog_product_entity_media_gallery on (
catalog_product_entity_media_gallery.value = t.value
)
the first gives an error and the second- and third one gives back every product.
Give this one a try:
select
*
from (
select
entity_id,attribute_id,value,
MIN(value_id) value_id
from catalog_product_entity_media_gallery
group by
entity_id,attribute_id,value
having COUNT(*) > 1
) A1
inner join catalog_product_entity_media_gallery A2 on
A1.entity_id = A2.entity_id and
A1.attribute_id = A2.attribute_id and
A1.value = A2.value and
A1.value_id = A2.value_id
You can just get the min id by value, then except the other records:
select
*
from catalog_product_entity_media_gallery t1
where exists
( select * from
(select value, min(value_id) as min_value_id
from catalog_product_entity_media_gallery
group by value
) as t2
where t1.value=t2.value and t1.value_id=t2.min_value_id
)
If you want delete the duplicated rows, change exists to not exists.
delete
from catalog_product_entity_media_gallery t1
where not exists
( select * from
(select value, min(value_id) as min_value_id
from catalog_product_entity_media_gallery
group by value
) as t2
where t1.value=t2.value and t1.value_id=t2.min_value_id
)
Is there a more efficient way of doing the following?
select *
from foo as a
where a.id = (select max(id) from foo where uid = a.uid group by uid)
group by uid;
)
This answer looks similar, but is this answer the best way of doing this - How to select the first row for each group in MySQL?
Thanks,
Chris.
P.S. the table looks like:
CREATE TABLE foo (
id INT(10) NOT NULL AUTO_INCREMENT,
uid INT(10) NOT NULL,
value VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`),
INDEX `uid` (`uid`)
)
data:
id, uid, value
1, 1, hello
2, 2, cheese
3, 2, pickle
4, 1, world
results:
id, uid, value
3, 2, pickle
4, 1, world
See http://www.barricane.com/2012/02/08/mysql-select-last-matching-row.html for more details.
Try this query -
SELECT t1.* FROM foo t1
JOIN (SELECT uid, MAX(id) id FROM foo GROUP BY uid) t2
ON t1.id = t2.id AND t1.uid = t2.uid;
Then use EXPLAIN to analyze queries.
SELECT t1.* FROM foo t1
LEFT JOIN foo t2
ON t1.id < t2.id AND t1.uid = t2.uid
WHERE t2.id is NULL;
Returning the last row of each GROUP BY in MySQL with WHERE clause:
SELECT *
FROM foo
WHERE id IN (
SELECT Max(id)
FROM foo
WHERE value='XYZ'
GROUP BY u_id
)
LIMIT 0,30
if table is big in size. Make view containing all last row id
create view lastrecords as (select max(id) from foo where uid = a.uid group by uid)
Now join your main query with this view. It will be faster.
SELECT t1.* FROM tablename as t1
JOIN lastrecords as t2
ON t1.id = t2.id AND t1.uid = t2.uid;
OR You can do join with last records direct in query also:
SELECT t1.* FROM tablename as t1
JOIN (SELECT uid, MAX(id) id FROM tablename GROUP BY id) as t2
ON t1.id = t2.id AND t1.uid = t2.uid;
This has worked for me thanks!
SELECT t1.* FROM foo t1
JOIN (SELECT uid, MAX(id) id FROM foo GROUP BY uid) t2
ON t1.id = t2.id AND t1.uid = t2.uid;
my version:
SELECT * FROM messages t1 JOIN (SELECT MAX(id) id FROM messages where uid = 8279 and actv=1 GROUP BY uid ) t2 ON t1.id = t2.id ORDER BY datex desc LIMIT 0,10;
this code below doesn't return all rows I want because if max id column is an inactive one then it skips the group even if the group has active rows..
select * from messages where uid = 8279 and actv=1 and id In (Select max(id) From messages Group By uid) order by datex desc;
The easiest way : you can select from selected list that is sorted.
SELECT * FROM (SELECT * FROM foo order by id DESC) AS footbl
group by uid
order by id DESC
This code works on me:
SELECT * FROM foo GROUP BY foo.uid
HAVING MAX(foo.id)