here is my query that makes an error for Join
$query = 'SELECT
a.ks_u_id,
a.ks_keyword,
b.u_photo
FROM
'.T_KEYWORD_HISTORY.' a
WHERE a.ks_u_id in ( SELECT uf_target_id FROM '.T_USER_FOLLOW.' WHERE uf_user_id="'.$u_id.'" and uf_target_id <> "'.$u_id.'" )
JOIN '.T_USER_ACCOUNT.' b ON b.u_id = a.ks_u_id
ORDER BY a.ks_time DESC
LIMIT 0 , 5 ';
I get this error message.
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 'JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id ORDER BY a.ks_time DESC ' at line 8
SELECT
a.ks_u_id, a.ks_keyword, b.u_photo
FROM T_KEYWORD_HISTORY a
WHERE a.ks_u_id in ( SELECT uf_target_id
FROM T_USER_FOLLOW
WHERE uf_user_id="jake" and uf_target_id <> "jake" )
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id
ORDER BY a.ks_time DESC
LIMIT 0 , 5
It looks like I wrote wrong query for JOIN command.
Try
SELECT
a.ks_u_id, a.ks_keyword, b.u_photo
FROM T_KEYWORD_HISTORY a
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id
WHERE a.ks_u_id in ( SELECT uf_target_id
FROM T_USER_FOLLOW
WHERE uf_user_id="jake" and uf_target_id <> "jake" )
ORDER BY a.ks_time DESC
LIMIT 0 , 5
The JOIN clause comes before the WHERE clause. Your query should be:
SELECT
a.ks_u_id, a.ks_keyword, b.u_photo
FROM T_KEYWORD_HISTORY a
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id
WHERE a.ks_u_id in ( SELECT uf_target_id
FROM T_USER_FOLLOW
WHERE uf_user_id="jake" and uf_target_id <> "jake" )
ORDER BY a.ks_time DESC
LIMIT 0 , 5
Related
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
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
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
this is my query.when i add order by desc in this query its gets an error pls help me.
SELECT *
FROM
(SELECT package_details.*,
r.state AS source_name,
d.state AS dest_name
FROM (`package_details`)
LEFT JOIN country_state_city r ON r.id=package_details.region_id
LEFT JOIN country_state_city d ON d.id=package_details.destination_id
WHERE `package_availability_type` = 'all'
AND `admin_status` = 'ACTIVE'
AND `status` = 'ACTIVE'
AND (package_name LIKE '%Istanbul%'
OR routes LIKE '%Istanbul%'
OR r.state LIKE '%Istanbul%'
OR d.state LIKE '%Istanbul%')
AND
ORDER BY `package_price` DESC
UNION ALL SELECT package_details.*,
r.state AS source_name,
d.state AS dest_name
FROM (`package_details`)
LEFT JOIN country_state_city r ON r.id=package_details.region_id
LEFT JOIN country_state_city d ON d.id=package_details.destination_id
WHERE `package_availability_type` ='range'
AND `admin_status` = 'ACTIVE'
AND `status` = 'ACTIVE'
AND `to_date` >= '2017-02-07'
AND (package_name LIKE '%Istanbul%'
OR routes LIKE '%Istanbul%'
OR r.state LIKE '%Istanbul%'
OR d.state LIKE '%Istanbul%')
AND
ORDER BY `package_price` DESC) AS dt LIMIT 0,100
error:
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 'ORDER BY package_price DESC UNION ALL SELECT package_details.*,r.state AS sour' at line 1
Remove AND Before Order By Clause
AND
ORDER BY `package_price` DESC
please can anyone spot what is wrong with this query.It is giving error but i can't really see where the error is from since it is not really specified
mysql>
insert into sorting (mindlrid,maxdlrid)
values (
(select min(dlrid)
from dlr
where sid=
(select distinct(s.sendid)
from sent s
,dlr d
where s.uid=d.uid
and d.d_billed=1
and d.d_sent=0
and s.processed=1
and s.sendid=d.sid
order by s.sendid asc
limit 1))
,(select max(dlrid)
from dlr
where sid=(
select distinct(s.sendid)
from sent s,dlr d
where s.uid=d.uid
and d.d_billed=1
and d.d_sent=0
and s.processed=1
and s.sendid=d.sid
order by s.sendid asc
limit 1)
);
ERROR 1064 (42000): 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 '' at line 1
You're missing a closing parenthesis. Probably at the very end of the insert. Equivalent: there is an extra opening paren on the values clause.
INSERT INTO sorting
(mindlrid,
maxdlrid)
VALUES (SELECT Min(dlrid)
FROM dlr
WHERE sid = (SELECT DISTINCT( s.sendid )
FROM sent s,
dlr d
WHERE s.uid = d.uid
AND d.d_billed = 1
AND d.d_sent = 0
AND s.processed = 1
AND s.sendid = d.sid
ORDER BY s.sendid ASC
LIMIT 1)),
(SELECT Max(dlrid)
FROM dlr
WHERE sid = (SELECT DISTINCT( s.sendid )
FROM sent s,
dlr d
WHERE s.uid = d.uid
AND d.d_billed = 1
AND d.d_sent = 0
AND s.processed = 1
AND s.sendid = d.sid
ORDER BY s.sendid ASC
LIMIT 1));
You can use the result of a SELECT directly, no need to wrap that into a VALUES clause:
insert into sorting (mindlrid,maxdlrid)
select min(dlrid), max(dlrid)
from dlr
where sid=
(select distinct s.sendid
from sent s
,dlr d
where s.uid=d.uid
and d.d_billed=1
and d.d_sent=0
and s.processed=1
and s.sendid=d.sid
order by s.sendid asc
limit 1)
Note that DISTINCT is not a function. It's an operator that includes all columns of the SELECT list.