I'm trying to create a kind of contacts list based on whether the user has a message from them or not.
I am trying this:
SELECT *
FROM chat
INNER JOIN user_info ON user_info.userID = chat.senderID
INNER JOIN user_login ON user_login.userID = chat.senderID
WHERE receiverID = :senderID
GROUP BY receiverID
ORDER BY chat.messageID ASC
What I would like to happen is having all the IDs that are the same grouped together so when I use a foreach loop it doesn't return the same ID multiple times.
However I receive this error from my code:
Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'username.chat.messageID' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by in"
I have tried searching it up and I found a few people saying to remove "only_full_group_by" in the sql mode variable. I cannot do this it gives me an error saying "#1227 - Access denied; you need (at least one of) the SUPER privilege(s) for this operation"
Any help on solving this would be great.
Related
I am trying to run a select statement from two tables, the data that I want to return takes on 3-4 joins to achieve. I am getting the error
ORA-00904: "ADDRESS_USAGES"."AUS_PRO_REFNO": invalid identifier
when both tables and columns exist. I have read the post relating to this error but given that I am just starting out I cant make head nor tail of them. Any suggestions (be gentle). SQL below TIA
select ins_srq_no, adr_line_all from inspections
join properties
on inspections.ins_pro_refno = properties.pro_propref
join addresses
on properties.pro_propref = address_usages.aus_pro_refno
join address_usages
on address_usages.aus_pro_refno = addresses.adr_refno
where fsc.address_usages.end_date is null;
This on clause is incorrect, because you haven't yet joined to the address_usages table: on properties.pro_propref = address_usages.aus_pro_refno. This is causing Oracle to throw the error you're seeing; it's not about the table or column not existing, it's the fact that within that query, that identifier is invalid because you haven't yet joined to the table.
At a guess (I can't be 100% sure without seeing your table structures and foreign keys), you need to join addresses back to properties. If so, the query should look something like this:
select ins_srq_no, adr_line_all
from inspections
inner join properties
on inspections.ins_pro_refno = properties.pro_propref
inner join addresses
on properties.pro_propref = addresses.[column name of FK to properties]
inner join address_usages
on address_usages.aus_pro_refno = addresses.adr_refno
where fsc.address_usages.end_date is null;
There is a lot of questions regarding this error message type, but I have not found -or maybe not understood- any answers on this specific case.
I have an application that can connect to custom database and retrieve data within a certain time span. Sort order can also be set.
Different views can be used to retrieve data and the application shall not have any knowledge of which columns that exists in the view.
But the application must know which column in the underlying table that shall be used to find the time span and which column in the underlying table that shall be used to set sort order.
How can I do that when I do not want this information to be returned as a part of the “select” statement?
/*--------- This is an example of view in the custom database ------------*/
CREATE VIEW [dbo].[EventViewFull]
AS
SELECT
dbo.[Events].[Time],
dbo.[Events].MicroSec,
dbo.[Events].Seq,
dbo.Redundancy.Redundancy,
dbo.OrigStation.OrigStation,
dbo.Priority.Priority,
dbo.EventType.EventType,
dbo.CommGrp.CommGrp,
dbo.AlarmState.AlarmState,
dbo.[Events].Acked,
dbo.Names1.Name1,
dbo.Names2.Name2,
dbo.[Events].EventText,
dbo.[Events].[Description],
dbo.[Events].AddInfo,
dbo.[Events].Members
FROM
dbo.[Events]
INNER JOIN dbo.Names1 ON dbo.[Events].Name1_ID = dbo.Names1.Name1_ID
INNER JOIN dbo.Names2 ON dbo.[Events].Name2_ID = dbo.Names2.Name2_ID
INNER JOIN dbo.AlarmState ON dbo.[Events].AlarmState_NR = dbo.AlarmState.AlarmState_NR
INNER JOIN dbo.EventType ON dbo.[Events].EventType_NR = dbo.EventType.EventType_NR
INNER JOIN dbo.CommGrp ON dbo.[Events].CommGrp_NR = dbo.CommGrp.CommGrp_NR
INNER JOIN dbo.Priority ON dbo.[Events].Priority_NR = dbo.Priority.Priority_NR
INNER JOIN dbo.OrigStation ON dbo.[Events].OrigStation_NR = dbo.OrigStation.OrigStation_NR
LEFT JOIN dbo.Redundancy ON dbo.[Events].OrigStation_NR = dbo.Redundancy.Redun
/*--This is the not working query in the application. SQL server, database name, time span and view is input from the user -----*/
select * from EventViewFull
where [Event].[dbo].[Events].UTCTime between '2012-11-18 23:0:0' and '2014-6-18 22:0:0'
order by [Event].[dbo].[Events].UTCTime DESC, [Event].[dbo].[Events].Seq DESC
/* Error messages I get with the current implementation of the script.
The multi-part identifier "Event.dbo.Events.UTCTime" could not be bound.
The multi-part identifier "Event.dbo.Events.Seq" could not be bound.
*/
I hope I understood your problem properly
You can use the following query to return all the columns only from the view
select EventViewFull.* from EventViewFull
join [Event].[dbo].[Events] ev ON EventViewFull.Seq = ev.Seq
where ev.UTCTime between '2012-11-18 23:0:0' and '2014-6-18 22:0:0'
order by ev.UTCTime DESC, ev.Seq DESC
This query will eliminate the error what you are getting at the moment. But I assumed Seq column as the primary key for the event. You need to change this to the correct column, if it is not the right one.
However as you can see the query, [Event].[dbo].[Events] is joined again with the view, even though the view has the information from the same table. It would be good to add UTCTime to the view, from the performance point of view, but not sure why does that not work for you.
I had to add UTCTime and Seq to the select statement in the [dbo].[EventViewFull] event though I did not want UTCtime and Seq to be a part of the resulting data set, but I can live with it.
I am trying to find an error in a massive SQL statement (not mine) - I have cut a lot of it out to make it readable - even pared down it still throws the error
SELECT DISTINCT Profiles.ID
FROM
(select * from Profiles RIGHT JOIN FriendList ON (FriendList.Profile = 15237)
order by LastLoggedIn DESC ) as Profiles
This returns an error
Duplicate column name 'ID'
I have tested the the last part (select * from Profiles ... order by LastLoggedIn DESC) and it works fine by itself
I have tried to troubleshoot by changing column names in the DISTINCT section without any luck.
One solution I read was to remove the DISTINCT, but that didn't help.
I just can't see where the duplicate column error can be coming from. Could it be a database integrity problem?
Any help much appreciated.
Your Profile and FriendList tables both have an ID column. Because you say select *, you're getting two columns named ID in the sub-select which is aliased to Profiles, and SQL doesn't know which one Profiles.ID refers to (note that Profiles here is referring to the alias of the sub-query, not the table of the same name).
Since you only need the ID column, you can change it to this:
SELECT DISTINCT Profiles.ID FROM
( select Profiles.ID from Profiles RIGHT JOIN FriendList ON (FriendList.Profile = 15237)
order by LastLoggedIn DESC ) as Profiles
Replace the "select *" with "select col1, col2..." and the error should become apparent (i.e. multiple columns named "ID"). Nothing to do with distinct or database integrity.
you have a table called Profiles and you are "creating" a temp table called Profiles in your From, that would be my guess as to what is causing the problem. call your temp bananas and try SELECT DISTINCT bananas.ID FROM and see if that works
As the error says, each of the tables that you're joining together has a column named ID. You'll have to specify which ID column you want (Profiles.ID or FriendList.ID) or include ID in the join conditions.
Profiles and FriendList both have an ID column. You are asking to call the entire join "Profiles", and then using Profiles.ID, but SQL doesn't know which ID you mean.
Query :
select i.Name,ri.Country,ri.State,ri.City
from Information as i
join ResidenceInformation as ri
order by Name
The error that i get is :
Error code -1, SQL state 42X01: Syntax error: Encountered "order" at line 4, column 5.
Line 1, column 1
Execution finished after 0 s, 1 error(s) occurred.
Why am i getting an error ?
The error is because you forgot to specify JOIN criteria, like this:
SELECT i.Name, ri.Country, ri.State, ri.City
FROM Information as i
JOIN ResidenceInformation as ri ON ri.column = i.column
ORDER BY Name
You need to replace column with the names of the appropriate columns that link the tables correctly for the output you need.
You should also specify the table alias in your ORDER BY, to protect against an ambiguous column reference error.
You get an error because your syntax is wrong: after join there needs to be on, like this:
select i.Name,ri.Country,ri.State,ri.City
from Information as i
join ResidenceInformation as ri
on ri.info_id=i.id -- <<< Added a join condition
order by Name
SQL needs to know how to "link up" the rows of the table that you are joining to the row(s) of the other table(s) in the query. I am assuming that ResidenceInformation has a foreign key into Information called info_id.
If the Name is present in both ResidenceInformation and Information, you need to prefix it with the table name or an alias. In fact, it's a good idea to do it anyway for added clarity.
I think you may have forgot to tell the join clause which columns to join against. You need to tell the database how these 2 tables connect to each other. Something like ON i.id = ri.InformationId
select i.Name,ri.Country,ri.State,ri.City
from Information as i
join ResidenceInformation as ri ON i.id = ri.InformationId
order by i.Name
Also, you may need the table alias in the order by clause, which I've added as well.
I've got a table with 500.000 records filled with Twitter updates. Then I've got a table with user info.
I basically need all the Twitter records of the people in my user table.
I can do it with this SELECT IN query:
SELECT *
FROM STATUS WHERE twitterUserID
IN (
SELECT twitteruserid
FROM accountLink
)
But that's obviously very slow.
I then tried to do it with a join, but it only shows 7 records. No idea why.
SELECT status . * , accountLink.userId, accountLink.twitterUserId
FROM status
JOIN accountLink
ON status.twitterUserId = accountLink.twitterUserId
Does anyone know what could cause this behaviour and how to solve it?
Try changing it to this:
SELECT status.* , accountLink.userId, accountLink.twitterUserId
FROM status
LEFT JOIN accountLink
ON status.twitterUserId = accountLink.twitterUserId
I suspect that there aren't matches for all the records between status and account link. Doing a left join will select every status regardless of whether or not accountLink has a match.
The JOIN syntax should work, unless the column data types are different.
Per the MySQL Documentation for IN():
The search for the item then is done using a binary search. This means IN is very quick if the IN value list consists entirely of constants. Otherwise, type conversion takes place according to the rules described in Section 11.2, “Type Conversion in Expression Evaluation”, but applied to all the arguments.
Ensuring that your column types match should ensure that the JOIN syntax works correctly.
SELECT s.*, a.twitterUserId, a.userId
FROM status AS s INNER JOIN accountLink AS a
WHERE s.twitterUserId=a.twitterUserId
You DO want to use inner join because you only want to return results IF the "status" table has a record AND a corresponding user record is found in the "accountLink" table. If a "status" table record does NOT have a corresponding user entry, you shouldn't display it (at least according to your post). LEFT OUTER JOIN would display status table records even if there was not a matching entry in the accountLink table.
Here's a great resource for learning about SQL joins:
SQL Joins (w3schools.com)