SQLite "<>" operator not working - sqlite

I have this query:
SELECT username FROM users INNER JOIN friends WHERE friends.user_a = users.id OR friends.user_b = users.id AND users.username <> 'david';
It is returning a row where users.username = 'david'. How can I have this not returned?
'users' table structure/sample data
'friends' table structure/sample data

Maybe try NOT LIKE '%david%' for any non-print chars?
try WHERE (friends.user_a = users.id OR friends.user_b = users.id) AND users.username <> 'david';
Explanation:
Your logic was friends.user_a = users.id OR friends.user_b = users.id AND users.username <> 'david'
You were saying: "bring me anyone who's friend with user_a OR etc..."
so when the first applies it doesn't need to check the rest, but after the parentheses, we are saying: "Bring me anyone who 1. Is friends with user_a or users_b 2. His user_name is not 'david'".

Related

Query performance issue - Not in

The query below is created by someone else but is running long so need to improve the performance. Can you please suggest me a better way to do?
Select * from ps_vendor G where G.vendor_id NOT IN (SELECT vendor_id
FROM ps_voucher_line
WHERE vendor_id <> '70830'
AND cntrct_id <> ' '
AND vendor_id NOT IN (SELECT vendor_id
FROM ps_cntrct_hdr
WHERE
cntrct_type = 'AP'))
Please try with inner join condition like display below
Select G.* from ps_vendor G
inner join ps_voucher_line pvl
on G.vendor_id = pvl.vendor_id and pvl.vendor_id <> '70830'
inner join ps_cntrct_hdr pch
on (pch.vendor_id <> pvl.vendor_id and pch.cntrct_type = 'AP')
where pv.cntrct_id <> ' '
Try different variations in joining condition!!
Good is that you have sub query to verify results.

SQLITE : inline views or nested subqueries?

I'm trying to write a query on two simple tables. Tables are simple, the query is not :)
Anyway...
Here is the database scheme :
and here is an overview of table content :
I'm trying to write a query that would list all assets in corresponding table, only if the are marked as "wanted" (meaning the boolean field asset_owned =0) and that are referenced for another owner as "owned".
This is what I have so far and it works :
SELECT
user.user_pseudo AS REQUESTER,
asset.asset_sku AS SKU,
asset.asset_name AS ASSET_NAME
FROM
asset
INNER JOIN user ON asset.id_user = user.id
WHERE
asset.asset_owned = 0
AND
asset.asset_sku IN (SELECT asset.asset_sku FROM asset WHERE asset.asset_owned = 1)
But, in the same query (if possible) I would like to get the owner name as well.
The first result of such a query on those table would be :
me,003,Test003,you.
I've tried inline SELECT and nested subqueries like :
SELECT
user.user_pseudo as ASKER,
asset.asset_sku as SKU,
asset.asset_name as NAME,
subquery1.user.user_pseudo as OWNER
FROM
asset
INNER JOIN user ON asset.id_user = user.id,
(SELECT user.user_pseudo.asset_asset_sku FROM asset INNER JOIN user ON asset.id_user = user.id WHERE asset.asset_owned = 1) subquery1
WHERE
asset.asset_owned = 0 AND
subquery1.asset.asset_sku IN (SELECT asset.asset_sku FROM asset INNER JOIN user ON asset.id_user = user.id WHERE asset.asset_owned=1)
but of course that does not work.
Thanks for any direction you could point me to.
happy new year
Mathias
So this was fun for me (I'm learning SQL, so this is good practice!) - I appreciate the very clear question.
Hopefully this works for you - I used two sub-queries (one each for 'owner' and 'requester') and then joined those on SKU and name. It works in SQLite with the small sample data shown above.
SELECT requester, subq1.SKU, subq1.name, owner
FROM
(SELECT pseudo AS requester, SKU, name
FROM asset, user
WHERE owned = 0
AND user.id = id_user) subq1,
(SELECT pseudo AS owner, SKU, name
FROM asset, user
WHERE owned = 1
AND asset.id_user = user.id) subq2
WHERE subq1.SKU = subq2.SKU
AND subq1.name = subq2.name;

DELETE from a Join in SQLite / syntax for IN clause on tuples?

I have a schema which uses a compound primary key to store users and associated data, like so:
CREATE TABLE users (
domain integer,
userid integer,
...
PRIMARY KEY (domain, userid)
);
CREATE TABLE userdata (
domain integer,
userid integer,
key integer,
...
PRIMARY KEY (domain, userid, key),
FOREIGN KEY (domain, userid) REFERENCES users(domain, userid)
);
I cannot make changes to this schema, nor can I opt to use another DBMS.
I want to delete some rows in userdata, based on a criterion over users. Conceptually, i'd like to do something like:
DELETE FROM userdata
WHERE (domain, userid) IN (
SELECT domain, userid FROM users WHERE <some condition>
) AND key = some_constant;
Or alternatively
DELETE ud FROM userdata ud
INNER JOIN users u on u.domain = ud.domain and u.userid = ud.userid
WHERE <some condition over u>
AND ud.key = some_constant
But sqlite3 (3.8.2) rejects both forms, the former with
Error: near ",": syntax error
and the latter with
Error: near "ud": syntax error
Annoyingly, the IN syntax works perfectly well when the join key consists of a single column.
What is the proper syntax or technique for achieving this ?
Not a SQLite user but I'd try selecting the ROWIDs to delete in userdata. I mean something like
delete from userdata
where rowid in (
select ud.rowid
from userdata ud
join users u on ...
where condition
)
I am supposing you want to delete key from user_data and x,y,z condition on user.
DELETE ud.key FROM user_data AS ud
LEFT JOIN users AS u ON ud.user_id = u.user_id
WHERE u.something='condition'
Your first attempt does not work because tuples (or whatever they are called) are not supported by SQLite (supported syntax here).
Your second attempt is invalid syntax as there is no JOIN in DELETE statements (maybe as an extension introduced by some DBMS but not in SQL92 apparently).
What you can try instead is:
DELETE FROM userdata
WHERE domain IN (SELECT u.domain FROM users u WHERE <some condition>)
AND userid IN (SELECT u.userid FROM users u WHERE <some condition> AND domain = u.domain)
AND key = some_constant;

SQL Server : Declare is causing problems in query

I currently have an issue with SQL Server which I can't figure out.
The error is:
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations
SQL:
DECLARE #IdUser INT
Select
#IdUser = Id,
Username,
(Select Count(*) From GagsLikes where Userid = #IdUser And Good = 1) as GagLikes,
(Select Count(*) From GagsViews where UserID = #IdUser),
(Select Count(*) From Gags Where UserID = #IdUser) as GagViews
From
Users
Order by
GagLikes, GagViews
Thanks in advance!
You may not use that variable as you do it try this:
Select
U.Id,
U.Username,
(Select Count(*) From GagsLikes where Userid = U.Id And Good = 1) as GagLikes,
(Select Count(*) From GagsViews where UserID = U.Id),
(Select Count(*) From Gags Where UserID = U.Id) as GagViews
From
Users AS U
Order by
GagLikes, GagViews
your table might return more then one rows . make sure that your table return only one row otherwise it can
t store multiple id's in to one int variable .

Drupal 6: "Facebook" style comment ordering (threaded, descending+ascending)

Working with Drupal 6, my goal is to order a set of threaded comments similar to how Facebook outputs comments: with the 'anchor' or first comment in each thread sorted DESC, and any 'internal' thread replies sorted ASC so the newest comments are at the bottom.
Here's the SQL query from comment_render, with ordering by COMMENT_ORDER_NEWEST_FIRST:
SELECT c.cid as cid, c.pid, c.nid,
c.subject, c.comment, c.format,
c.timestamp, c.name, c.mail,
c.homepage, u.uid, u.name AS
registered_name, u.signature,
u.picture, u.data, c.thread, c.status
FROM {comments} c INNER JOIN {users} u
ON c.uid = u.uid WHERE c.nid = 141737
AND c.status = 0 ORDER BY c.thread
DESC
This returns all comments, ordered by the newest thread first:
03/
03.00/
02/
02.06/
02.05/
02.04/
02.03/
02.01/
02.00/
01/
The desired ordering in my case is this:
03/
03.00/
02/
02.00/
02.01/
02.02/
02.03/
02.04/
02.05/
02.06/
01/
Again just think of the Facebook wall and you get the idea.
Can anyone assist in enhancing the SQL query appropriately? In my case (but perhaps not in all cases) the thread depth is forcibly limited to 1 via a custom module.
One other note - in my case it only has to work under MySQL.
Well this query works, but again I'm a SQL noob so this probably isn't fully correct.
SELECT c.cid as cid, c.pid, c.nid,
c.subject, c.comment, c.format,
c.timestamp, c.name, c.mail,
c.homepage, u.uid, u.name AS
registered_name, u.signature,
u.picture, u.data, c.thread, c.status
FROM (SELECT c.cid as cid, c.pid,
c.nid, c.subject, c.comment, c.format,
c.timestamp, c.name, c.mail,
c.homepage, u.uid, u.name AS
registered_name, u.signature,
u.picture, u.data, c.thread, c.status
FROM comments c INNER JOIN users u
ON c.uid = u.uid WHERE c.nid = 141737
AND c.status = 0 ORDER BY
SUBSTRING(thread, 1, (LENGTH(thread) -
1))) c INNER JOIN users u ON c.uid =
u.uid WHERE c.nid = 141737 AND
c.status = 0 ORDER BY
SUBSTRING(thread, 1, 2) DESC
The idea is to first query with an ordering based on the full thread (02.05, etc) and then do another SELECT on just the first two characters of the thread field. Can I get a bit of help optimizing this or is it otherwise "correct"?

Resources