Results returning twice from MySQL query - mysql

I'm struggling to figure out why my results are returning twice for the group messages. It's returning the correct values for the single conversations.
It should be returning all the data from Data in table screenshot. However it's returning the data with is_group = 1 twice.
Data in Table:
MySQL Query:
(SELECT dm.* FROM `direct_message`AS dm
INNER JOIN direct_message_thread AS dmt
ON dmt.chat_id = dm.id
WHERE
( dm.recipient_id = '10896' OR dm.creator_id = '10896' )
AND dm.school_id = '1'
GROUP BY dm.id
ORDER BY dmt.inserted DESC
) UNION ALL (
SELECT dm.* FROM `direct_message` AS dm
INNER JOIN direct_message_thread AS dmt ON dmt.chat_id = dm.id
LEFT JOIN direct_message_group AS dmg ON dmg.chat_id = dm.id
WHERE dmg.staff_id = '10896' AND dm.school_id = '1'
GROUP BY dm.id
ORDER BY dmt.inserted DESC
) LIMIT 0, 25
Results:
I think it could be because of the first SELECT getting the results and then the UNION ALL get the same results but not grouping together with the first SELECT
When I try and do the following:
(SELECT dm.* FROM `direct_message`AS dm
INNER JOIN direct_message_thread AS dmt
ON dmt.chat_id = dm.id
WHERE ( dm.recipient_id = '10896' OR dm.creator_id = '10896' )
AND dm.school_id = '1'
) UNION ALL (
SELECT dm.* FROM `direct_message` AS dm
INNER JOIN direct_message_thread AS dmt ON dmt.chat_id = dm.id
LEFT JOIN direct_message_group AS dmg ON dmg.chat_id = dm.id
WHERE dmg.staff_id = '10896' AND dm.school_id = '1'
)
GROUP BY dm.id
ORDER BY dmt.inserted DESC
LIMIT 0, 25
It shows this error message:
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 'GROUP BY dm.id ORDER BY dmt.inserted DESC LIMIT 0, 25' at line
14

You are using union for two queries.
Records 595-597 and 599-601 are met both of them.
My be better to select to whole query with
select id,...
from (...)
group by id

Related

Multiple-table UPDATE with WHERE not working

Can someone help me understand why below UPDATE query produces an ERROR?
WITH subt AS (
SELECT t.portfolio, s.isin, t.quantity, t.date
FROM transactions t
JOIN stocks s
ON t.stock = s.name
ORDER BY t.id DESC
LIMIT 1
)
UPDATE holdings h
JOIN subt
ON h.portfolio = subt.portfolio
AND h.isin = subt.isin
SET h.end_date = DATE_SUB(subt.date, INTERVAL 1 DAY)
WHERE h.end_date is NULL
The error I get:
SQL Error (1064): You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'UPDATE holdings h JOIN subt ON h.portfolio =
subt.portfolio AND h.isin ' at line 10
While below SELECT works fine:
WITH subt AS (
SELECT t.portfolio, s.isin, t.quantity, t.date
FROM transactions t
JOIN stocks s
ON t.stock = s.name
ORDER BY t.id DESC
LIMIT 1
)
SELECT h.*
FROM holdings h
JOIN subt
ON h.portfolio = subt.portfolio
AND h.isin = subt.isin
WHERE h.end_date is NULL
I'm working with a MariaDB 10 Database via HeidiSQL
Instead of a WITH result you could try using a normal inner join on subquery
UPDATE holdings h
JOIN
(
SELECT t.portfolio, s.isin, t.quantity, t.date
FROM transactions t
JOIN stocks s
ON t.stock = s.name
ORDER BY t.id DESC
LIMIT 1
) subt
ON h.portfolio = subt.portfolio
AND h.isin = subt.isin
SET h.end_date = DATE_SUB(subt.date, INTERVAL 1 DAY)
WHERE h.end_date is NULL

how to use select sub query in other query MySQL

I am writing a mysql select sub query, that is working fine, it returns 2 column is there way to select only 1 column.
My query is
SELECT sum(fl.qunt) as qunt,(
SELECT GROUP_CONCAT( xp.id
SEPARATOR ',' )
FROM prdt AS xp
LEFT JOIN prdt_fac AS pf ON pf.fk_product_children = xp.rowid
WHERE pf.prdt_fat = p.id
AND pf.prdt_ch = 6953
GROUP BY pf.prdt_fat
LIMIT 0 , 1
) AS prdt_chd
FROM fac_log AS fl
LEFT JOIN fac AS f ON fl.fac = f.id
LEFT JOIN prdt AS p ON f.prdt = p.id
GROUP BY prod_child
ORDER BY fl.tms DESC
LIMIT 0 , 1
This return two column qunt and prdt_ch.
But i want only column in result qunt.
Is there any way, becuase inner select query is must be used to get correct result.
Main purpose of this query is that i have to use this query as sub query in an other query, in this condition it throws error "operand should contain 1 column"
Thanks in advance
just select qunt using your select query as a table (t)
select qunt from (
SELECT sum(fl.qunt) as qunt,(
SELECT GROUP_CONCAT( xp.id
SEPARATOR ',' )
FROM prdt AS xp
LEFT JOIN prdt_fac AS pf ON pf.fk_product_children = xp.rowid
WHERE pf.prdt_fat = p.id
AND pf.prdt_ch = 6953
GROUP BY pf.prdt_fat
LIMIT 0 , 1
) AS prdt_chd
FROM fac_log AS fl
LEFT JOIN fac AS f ON fl.fac = f.id
LEFT JOIN prdt AS p ON f.prdt = p.id
GROUP BY prod_child
ORDER BY fl.tms DESC
LIMIT 0 , 1 ) t

Golang GORP - No error, no results

dbCount, er2 = dbmap.SelectInt("SELECT COUNT(*) AS N FROM (SELECT av.id FROM Table av LEFT JOIN Table2 dt ON dt.`app_id` = av.`app_id` LEFT JOIN Table3 a ON a.`id` = av.`app_id` WHERE dt.`user_id` IN (?) OR a.`org_id` IN (?) GROUP BY av.id LIMIT ? OFFSET ?) tmp", user_id, org_ids, limit, offset)
user_id = 1000
org_ids = 13444,12444,10333
limit = 25
offset = 0
Any idea why this returns no error or results in GO but when run directly on the database returns 1 result? There are no errors either.

MySQL QUERY runs on localhost, but does not on server

I have this QUERY:
select
a.*
from
mt_proyecto a,
mt_mockup b,
mt_diseno c,
mt_modulo d
where
a.estado = 'A' and
(
(b.encargado = '1' and b.idproyecto = a.idmtproyecto) or
(c.encargado = '1' and c.idproyecto = a.idmtproyecto) or
(d.encargado = '1' and d.idproyecto = a.idmtproyecto)
)
group by
a.idmtproyecto
order by a.finalizado asc, a.feccrea desc
Result:
Then, I run the same code on server with the same database:
Is there any problem with the query?
It seems that the query is running correctly on your server, and returning no rows. Please make sure you have the same table contents on your local machine and your server.
Other things:
Pro tip: never use SELECT * or SELECT table.* in software. Always enumerate the columns you want in your result set.
Unless you use GROUP BY with aggregate functions like SUM() or `COUNT(), and naming the correct columns from the result set, it returns unpredictable results. Read this. http://dev.mysql.com/doc/refman/5.1/en/group-by-extensions.html
I solved with this QUERY:
select
a.*
from
(
mt_proyecto a
left join mt_mockup b on
b.idproyecto = a.idmtproyecto
left join mt_diseno c on
c.idproyecto = a.idmtproyecto
left join mt_modulo d on
d.idproyecto = a.idmtproyecto
left join mt_integracion e on
e.idproyecto = a.idmtproyecto
left join mt_pruebas_internas f on
f.idproyecto = a.idmtproyecto
)
where
a.estado = 'A' and
(
(a.idmtproyecto = b.idproyecto and
b.encargado = '1' ) or
(a.idmtproyecto = c.idproyecto and
c.encargado = '1' ) or
(a.idmtproyecto = d.idproyecto and
d.encargado = '1' ) or
(a.idmtproyecto = e.idproyecto and
e.encargado = '1' ) or
(a.idmtproyecto = f.idproyecto =
f.encargado = '1' )
)
group by a.idmtproyecto
order by
a.finalizado asc, a.feccrea desc
Thanks everyone for answer me. I'll do your suggestions .

MYSQL count child table with other modifiers - help please!

I have a ticket purchasing database and want to collect all of the orders that meet certain criteria, and then also grab the total number of tickets each order has by counting the rows in the 'orders_tickets' table that match the orderID. If I make the SQL a simple call, this SQL works:
SQL A:
SELECT o . * , COUNT( ot.OrderTicketID ) AS numtix
FROM orders o
LEFT JOIN orders_tickets ot ON o.orderID = ot.orderID
GROUP BY o.orderID
LIMIT 0 , 30
And this SQL works too - this is the original SQL call with all the data I want except the numtix data:
SQL B:
SELECT o.*,
IF(o.orderedbyID = '0', ob.fname, u.fname) AS obfname,
IF(o.orderedbyID = '0', ob.lname, u.lname) AS oblname
FROM orders o, perfs p, orders_orderedby ob, users u
WHERE p.eventID = '2'
AND p.perfID = o.perfID
AND ( (UNIX_TIMESTAMP(p.perfdatetime) - UNIX_TIMESTAMP(NOW())) > 0)
AND ob.orderID = o.orderID
AND u.userID = o.orderedbyID
ORDER BY p.perfdatetime ASC
LIMIT 0, 30
But when I try to incorporate SQL A into SQL B, I get errors:
SQL C: (does not work)
SELECT o.*, COUNT( ot.OrderTicketID ) AS numtix,
IF(o.orderedbyID = '0', ob.fname, u.fname) AS obfname,
IF(o.orderedbyID = '0', ob.lname, u.lname) AS oblname
FROM orders o, perfs p, orders_orderedby ob, users u
LEFT JOIN orders_tickets ot ON o.orderID = ot.orderID
WHERE p.eventID = '2'
AND p.perfID = o.perfID
AND ( (UNIX_TIMESTAMP(p.perfdatetime) - UNIX_TIMESTAMP(NOW())) > 0)
AND ob.orderID = o.orderID
AND u.userID = o.orderedbyID
ORDER BY p.perfdatetime ASC
GROUP BY o.orderID
LIMIT 0, 30
This is the error I get:
#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 'GROUP BY o.orderID LIMIT 0, 30' at line 12
My instinct is that the "GROUP BY" needs to apply to the LEFT JOIN and not the entire SQL, but that's a sheer guess.
Any help is most appreciated!
ORDER BY goes after GROUP BY:
GROUP BY o.orderID
ORDER BY p.perfdatetime ASC
LIMIT 0, 30
Update:
SELECT o.*,
COUNT( ot.OrderTicketID ) AS numtix,
IF(o.orderedbyID = '0', ob.fname, u.fname) AS obfname,
IF(o.orderedbyID = '0', ob.lname, u.lname) AS oblname
FROM orders o
JOIN perfs p
ON p.perfID = o.perfID
JOIN orders_orderedby ob
ON ob.orderID = o.orderID
JOIN users u
ON u.userID = o.orderedbyID
LEFT JOIN
orders_tickets ot
ON ot.orderID = o.orderID
WHERE p.eventID = '2'
AND p.perfdatetime < NOW()
GROUP BY
o.orderID
ORDER BY
p.perfdatetime ASC
LIMIT 0, 30