I'm having a problem with the result obtained on a select in my sqlite.
I already have a database fed, and I'm doing queries in my application in adobe air. In my table I have 6 columns:
id | name | email | CITY_ID | state_id | phone
When I do a select of the entire table, it returns me an array of objects.
result[30].id = 30;
result [30]. name = John;
result [30]. email = john#xxx.com;
result [30]. city_id = 1352;
result [30]. state_id = 352;
result [30]. phone = xxxxxxxxx;
All information came right, but the id value is incorrect ( correct is not 30 ) . It seems to me that i'm getting the numerical order and not getting the id column value.
Has anyone had this problem?
UPDATE
My query is:
_selectStat = new SQLStatement();
_selectStat.addEventListener( SQLEvent.RESULT, onDataLoaded );
_selectStat.addEventListener( SQLErrorEvent.ERROR, onSqlError );
_selectStat.sqlConnection = _connection;
var sql:String = 'SELECT * FROM "main"."agencia"';
_selectStat.text = sql;
_selectStat.execute();
I'm not familiar with Adobe development or sqlite, so I'm speculating here. If 'id' is a database property, then you may need to indicate that you want the column 'id', and not the property 'id'. There should be a way to do this with the adobe application syntax or the sqlite SQL syntax. In mssql, brackets [] are used for this, so it would be [id] to indicate the column 'id' and not the property. There should be something similar for your environment.
Related
I have a table as shown
ID (int) | DATA (bytea)
1 | \x800495356.....
The contents of the data column have been stored via a python script
result_dict = {'datapoint1': 100, 'datapoint2': 2.334'}
table.data = pickle.dumps(result_dict)
I can easily read the data back using
queried_dict = pickle.loads(table.data)
But I don't know how to query it directly as a json or even as plain text in postgres alone. I have tried the following query and many versions of it but it doesn't seem to work
-- I don't know what should come between SELECT and FROM
SELECT encode(data, 'escape') AS res FROM table WHERE id = 1;
-- I need to get this or somewhere close to this as the query result
res |
{"datapoint1": 100, "datapoint2": 2.33}
Thanks a lot in advance to everyone trying to help.
Original table has 1466303 records in it, I have inserted 1108441 of those records in to a separate table. What I would like to know is what data is left over? So I have made a query using multiple exists to find the data that was left:
SELECT SG_customer,
PHONE,
SG_Name,
SG_Secondary_Address,
SG_Primary_Address,
SG_City,
SG_State,
SG_Zip,
SG_Email
FROM FMJ_DB_VPI_EXPANDED_DATA X
WHERE NOT EXISTS (SELECT 1
FROM FMJScore
WHERE SGID = X.SG_Customer
AND Phone = X.Phone
AND Name = X.SG_Name
AND SecondAddress = X.SG_Secondary_Address
AND Address = X.SG_Primary_Address
AND City = X.SG_City
AND State = X.SG_State
AND Zip = X.SG_Zip
AND Email = X.SG_Email)
Running this returns back 144391 records, there should be a difference of 357862, I don't understand why its returning back so many records.
I assume you want null to be treated equal to null, I also assume that '' is not used as a value, if it is replace it with something that does not normally occur:
SELECT SG_customer,
PHONE,
SG_Name,
SG_Secondary_Address,
SG_Primary_Address,
SG_City,
SG_State,
SG_Zip,
SG_Email
FROM FMJ_DB_VPI_EXPANDED_DATA X
WHERE NOT EXISTS (SELECT 1
FROM FMJScore
WHERE coalesce(SGID,'') = coalesce(X.SG_Customer,'')
AND coalesce(Phone,'') = coalesce(X.Phone,'')
AND coalesce(Name,'') = coalesce(X.SG_Name,'')
AND coalesce(SecondAddress,'') = coalesce(X.SG_Secondary_Address,'')
AND coalesce(Address,'') = coalesce(X.SG_Primary_Address,'')
AND coalesce(City,'') = coalesce(X.SG_City,'')
AND coalesce(State,'') = coalesce(X.SG_State,'')
AND coalesce(Zip,'') = coalesce(X.SG_Zip,'')
AND coalesce(Email,'') = coalesce(X.SG_Email,''))
The optimizer might not be able to use indexes efficiently due to the function call
I'm working with PeopleSoft Campus Solutions, and we need to update about 22,000 rows of data. This is data between the tables of ACAD_PLAN_VW and ACAD_PROG. Students are listed on both, so their IDs match between the two.
Basically what we are trying to do is say that when the ID, academic career, student career number, effective sequence, and effective date match, AND where the academic plan (their degree, as stored on the ACAD_PLAN_VW) is a specific value, update the ACAD_PROG on the ACAD_PROG table to X value.
I tried to do some very interesting combinations of FROM statements, constantly getting errors. After some researching, I found out that SQLTools doesn't really like FROM statements within UPDATE statements, so I rewrote it to just make the connections manually. I'm assuming I'm doing this right, unless I need to reword it.
The statement I have is:
UPDATE PS_ACAD_PROG SET PS_ACAD_PROG.ACAD_PROG = 'UGDS'
WHERE PS_ACAD_PLAN_VW.EMPLID = PS_ACAD_PROG.EMPLID
AND PS_ACAD_PLAN_VW.ACAD_CAREER = PS_ACAD_PROG.ACAD_CAREER
AND PS_ACAD_PLAN_VW.STDNT_CAR_NBR = PS_ACAD_PROG.STDNT_CAR_NBR
AND PS_ACAD_PLAN_VW.EFFSEQ = PS_ACAD_PROG.EFFSEQ
AND PS_ACAD_PLAN_VW.EFFDT = PS_ACAD_PROG.EFFDT
AND PS_ACAD_PLAN_VW.ACAD_PLAN = 'DSTDS'
Theoretically, I would assume that this would update any student who has those connections. However, the error that I'm currently getting is as follows:
ORA-00904: "PS_ACAD_PLAN_VW"."ACAD_PLAN": invalid identifier
I have, as of yet, been unable to figure out the issue. I do have the correct access to view and update those fields, and the field does indeed exist.
Oracle doesn't know it should use the PS_ACAD_PLAN_VW table. Somehow you should reference it.
For example can you try this way?
UPDATE (
select
PS_ACAD_PROG.ACAD_PROG,
PS_ACAD_PLAN_VW.ACAD_PLAN
from
PS_ACAD_PROG,
PS_ACAD_PLAN_VW
where
PS_ACAD_PLAN_VW.EMPLID = PS_ACAD_PROG.EMPLID
AND PS_ACAD_PLAN_VW.ACAD_CAREER = PS_ACAD_PROG.ACAD_CAREER
AND PS_ACAD_PLAN_VW.STDNT_CAR_NBR = PS_ACAD_PROG.STDNT_CAR_NBR
AND PS_ACAD_PLAN_VW.EFFSEQ = PS_ACAD_PROG.EFFSEQ
AND PS_ACAD_PLAN_VW.EFFDT = PS_ACAD_PROG.EFFDT
)
SET
ACAD_PROG = 'UGDS'
WHERE
ACAD_PLAN = 'DSTDS'
Try to use the table which is not getting identified,by keeping in where clause so that you can use the select statement.That will help identifying the tables.
The following implementation should work:
UPDATE PS_ACAD_PROG
SET ACAD_PROG = 'UGDS'
WHERE ROWID IN(SELECT PROG.ROWID
FROM PS_ACAD_PROG PROG
, PS_ACAD_PLAN_VW PLAN
WHERE PLAN.EMPLID = PROG.EMPLID
AND PLAN.ACAD_CAREER = PROG.ACAD_CAREER
AND PLAN.STDNT_CAR_NBR = PROG.STDNT_CAR_NBR
AND PLAN.EFFSEQ = PROG.EFFSEQ
AND PLAN.EFFDT = PROG.EFFDT
AND PLAN.ACAD_PLAN = 'DSTDS');
I have the following table in PostgreSQL database:
CREATE TABLE maclist (
username character varying,
mac macaddr NOT NULL,
CONSTRAINT maclist_user_mac_key UNIQUE (username, mac)
);
I need a way to check if the MAC address is assigned to the user or the user has no assigned MAC addresses at all. Basically I need a query that returns row if all conditions are true or no condition is true ie NOT a XOR b.
EDIT:
Example:
username | mac
john | 11:22:33:44:55:66
john | 11:22:33:44:55:67
doe | 11:22:33:44:55:68
If I query:
username = john, mac = 11:22:33:44:55:66 -> true, 1 whatever...
username = john, mac != 11:22:33:44:55:66 -> 0, null or nothing...
username = jane, mac = no matter what except john's or doe's -> true, 1 whatever...
username = jane, mac = john's or doe's -> 0, null or nothing...
I need true under two conditions:
There is a row for that (user, mac) combination. There are no rows
for that user AND there are no rows for that mac
So far I got this:
SELECT yes
FROM
(SELECT 1 AS yes) AS dummy
LEFT JOIN maclist ON (username = 'user'
OR mac = '11:22:33:44:55:66')
WHERE ((username = 'user'
AND mac = '11:22:33:44:55:66')
OR (username IS NULL
AND mac IS NULL);
It works, but it seems to me like a hack and I also have no idea about the performance of this query as the database grows.
My question is if there are any better ways to do this.
EDIT: I've revised the logic slightly, I think it matches what you want
The following code will return 1 under two conditions...
There is a row for that user and that mac
There are 0 rows for that user and there are 0 rows for that mac
Under all other conditions, the query returns 0.
There are rows for that user but none of them are for that mac
There are rows for that mac but none of them are for that user
SELECT
CASE WHEN COUNT(*) = 0 THEN 1
WHEN SUM(CASE WHEN mac = '11:22:33:44:55:66'
AND user = 'user' THEN 1
ELSE 0 END) = 1 THEN 1
ELSE 0
END
FROM
maclist
WHERE
username = 'user'
OR mac = '11:22:33:44:55:66'
You can write the query simply as:
prepare query_mac(text, macaddr) as
select exists (select 1 from maclist where username = $1 and mac = $2)
or (not exists (select 1 from maclist where username = $1)
and not exists (select 1 from maclist where mac = $2)
);
PostgreSQL will evaluate each of the three queries separately, and use the unique index on (username,mac) for the first two. If you need to, you can add an index on (mac) for the third.
Practically all databases won't use an index when an OR is involved, so avoid those in the joins.
Edited:
It seems you are really making two separate queries:
who is the mac assigned to (if any)
what mac is assigned to the user (if any)
so a union won't be as easy as a collection of subselects. Use separate queries to split up the info into their own simple query, and combine them into one:
select
(select username from maclist
where mac = '11.22:33:44:55:66') as mac_assigned_to,
(select mac from maclist
where username = 'user') as user_assigned_to
Both columns may have nulls if there is nothing currently assigned, otherwise they'll show what's assigned for each concern.
Having a mental block with going around this query.
I have the following tables:
review_list: has most of the data, but in this case the only important thing is review_id, the id of the record that I am currently interested in (int)
variant_list: model (varchar), enabled (bool)
variant_review: model (varchar), id (int)
variant_review is a many to many table linking the review_id in review_list to the model(s) in variant_list review and contains (eg):
..
test1,22
test2,22
test4,22
test1,23
test2,23... etc
variant_list is a list of all possible models and whether they are enabled and contains (eg):
test1,TRUE
test2,TRUE
test3,TRUE
test4,TRUE
what I am after in mysql is a query that when given a review_id (ie, 22) will return a resultset that will list each value in variant_review.model, and whether it is present for the given review_id such as:
test1,1
test2,1
test3,0
test4,1
or similar, which I can farm off to some webpage with a list of checkboxes for the types. This would show all the models available and whether each one was present in the table
Given a bit more information about the column names:
Select variant_list.model
, Case When variant_review.model Is Not Null Then 1 Else 0 End As HasReview
From variant_list
Left join variant_review
On variant_review.model = variant_list.model
And variant_review.review_id = 22
Just for completeness, if it is the case that you can have multiple rows in the variant_review table with the same model and review_id, then you need to do it differently:
Select variant_list.model
, Case
When Exists (
Select 1
From variant_review As VR
Where VR.model = variant_list.model
And VR.review_id = 22
) Then 1
Else 0
End
From variant_list