using SELECT inside INSERT query - mysql

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.

Related

GROUP BY in SQL query is giving syntax error when used with Order By

I have this query:
SELECT customer.cus_id,customer.fname,customer.lname,customer.image, message.message ,message.msg_id,message.from_cus,message.to_cus, message.sent_time
FROM customer
INNER JOIN message ON cus_id=message.from_cus OR cus_id=message.to_cus
WHERE customer.cus_id IN ( 31,26)
AND (
from_cus =25
OR to_cus = 25
)
AND customer.cus_id NOT IN(25)
ORDER BY message.sent_time DESC
GROUP BY customer.cus_id ;
where 31, 26 are the cus_id of the customers whose last messages I want to fetch.I am getting all the messages sorted asscording to time between the 1 and ( 31,26 ) customer if I remove GROUP BY constraint.
But adding it gives me this syntax error
[Error code:1064 SQL state: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 'GROUP BY customer.cus_id' at line 11
I have disabled ONLY_FULL_GROUP_BY SQL mode.
Still I dont know How to fetch the last message.
This is not an aggregation operations, this is a filtering operations.
Use window functions:
SELECT cm.* FROM (
SELECT customer.cus_id,customer.fname,customer.lname,customer.image, message.message ,message.msg_id,message.from_cus,message.to_cus, message.sent_time,
ROW_NUMBER() OVER (PARTITION BY customer.cus_id ORDER BY message.sent_time DESC) as seqnum
FROM customer
INNER JOIN message ON cus_id=message.from_cus OR cus_id=message.to_cus
WHERE customer.cus_id IN ( 31,26)
AND (
from_cus =25
OR to_cus = 25
)
AND customer.cus_id NOT IN(25)
) cm
WHERE seqnum = 1
ORDER BY msg_id DESC;
Group by clause will be before order by clause
SELECT customer.cus_id,customer.fname,customer.lname,customer.image, message.message ,message.msg_id,message.from_cus,message.to_cus, message.sent_time
FROM customer
INNER JOIN message ON cus_id=message.from_cus OR cus_id=message.to_cus
WHERE customer.cus_id IN ( 31,26)
AND (
from_cus =25
OR to_cus = 25
)
AND customer.cus_id NOT IN(25)
GROUP BY customer.cus_id
ORDER BY message.msg_id DESC

Results returning twice from MySQL query

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

SQL help on subqueries and table joins

Need some sql help...using MYSQL...I've got some legacy code and a situation to resolve...This code below only returns occupants who have a record in the p_occupants_insurance table. I want to be able to list all the occupants regardless if they have a record in the p_occupants_insurance table or not.
SELECT a.occupants_insurance_id, a.occupant_id, a.policy_nbr, a.policy_type, a.coverage_amount_curr,
CASE
WHEN a.effective_date = '0000-00-00' THEN NULL
ELSE a.effective_date
END as effective_date,
CASE
WHEN a.expiration_date = '0000-00-00' THEN NULL
ELSE a.expiration_date
END as expiration_date,
a.insurance_company, a.custom1_label, a.custom2_label, a.custom3_label, a.custom1, a.custom2, a.custom3, c.name as prop_name, (SELECT x.name FROM portfolio_hierarchy x WHERE x.leaf_node_portf_id = d.portfolio_id ) as p_name, b.name as occupant_name, b.primary_contact, b.phone
FROM p_occupants_insurance a, p_occupants b, properties c, portfolio d
WHERE a.occupant_id = b.occupant_id
AND b.property_id = c.properties_id
AND c.portfolio_id = d.portfolio_id
AND d.account_id = 1
AND b.archived = 0
AND b.trashbin = 0
ORDER BY d.p_name ASC, prop_name ASC, occupant_name ASC, insurance_company ASC, policy_nbr ASC;
I know I can do subqueries like this:
SELECT b.name as occupant_name, b.primary_contact, b.phone,
(SELECT a.occupants_insurance_id FROM p_occupants_insurance a WHERE a.occupants_id = b.occupants_id) as occupants_insurance_id
FROM p_occupants b, properties c, portfolio d
WHERE a.occupant_id = b.occupant_id
AND b.property_id = c.properties_id
AND c.portfolio_id = d.portfolio_id
AND d.account_id = 1
AND b.archived = 0
AND b.trashbin = 0
ORDER BY d.p_name ASC, prop_name ASC, occupant_name ASC, insurance_company ASC, policy_nbr ASC;
But that is going to lead to a subquery for each column I want out of the p_occupants_insurance table. Is there a better way to accomplish this and can you help me write out the SQL? Sorry, SQL is not my strongest point.
Thanks for the help.
Without seeing the schema for each of the tables involved, and relying only on the sample SQL statement, it is difficult to say for sure, but I believe the following will return the result you are looking for:
SELECT
a.`occupants_insurance_id`,
a.`occupant_id`,
a.`policy_nbr`,
a.`policy_type`,
a.`coverage_amount_curr`,
IF(a.`effective_date` = '0000-00-00',NULL,a.`effective_date`) as a.`effective_date`,
IF(a.`expiration_date` = '0000-00-00',NULL,a.`expiration_date`) as a.`expiration_date`,
a.`insurance_company`,
a.`custom1_label`,
a.`custom2_label`,
a.`custom3_label`,
a.`custom1`,
a.`custom2`,
a.`custom3`,
c.`name` as `prop_name`,
(SELECT x.name FROM portfolio_hierarchy x WHERE x.leaf_node_portf_id = d.portfolio_id ) as `p_name`,
b.`name` as `occupant_name`,
b.`primary_contact`,
b.`phone`
FROM `p_occupants` b
LEFT JOIN `p_occupants_insurance` a
ON a.`occupant_id` = b.`occupant_id`
JOIN `properties` c
ON b.`property_id` = c.`properties_id`
JOIN `portfolio` d
ON c.`portfolio_id` = d.`portfolio_id`
WHERE d.account_id = 1
AND b.archived = 0
AND b.trashbin = 0
ORDER BY d.p_name ASC,
`prop_name` ASC,
`occupant_name` ASC,
`insurance_company` ASC,
`policy_nbr` ASC;

MySql Query Is Not Working Giving me an error for offset

Here's my sql Query
SELECT tl.*
FROM tbl_listing tl
LEFT
JOIN tbl_sub_category tsc
ON tl.subcategory = tsc.id
WHERE status_mode = 1
AND transaction_complete = 1
AND is_deleted != 1
AND tl.status IN (10,11,28,12)
ORDER
BY tl.status ASC
OFFSET 1
And here is the 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 'offset 1' at line 12
Thanks in advance
Basically, replacing OFFSET with LIMIT should be enough.
select tl.* from tbl_listing as tl left join `tbl_sub_category` as tsc on
tl.subcategory = tsc.id where status_mode = '1' and transaction_complete = '1' and
is_deleted != '1' and tl.status in (10,11,28,12) ORDER BY tl.status ASC LIMIT 1
You can use additional parameter in LIMIT like LIMIT 0, 2 to limit records starting from 0 to 2.
offset must be used in conjunction with limit
query should be
select tl.*
from tbl_listing as tl
left join `tbl_sub_category` as tsc on tl.subcategory = tsc.id
where status_mode = '1'
and transaction_complete = '1'
and is_deleted != '1'
and tl.status in (10,11,28,12)
ORDER BY tl.status ASC
LIMIT 10 OFFSET 1;

mysql query Join table doesn't work

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