let me show you a simple presentation on database structure:
Database structure
Query's parameters:
-id_category
-id_language
-id_account
I need to select all rows from wordconfigs table where id_wordconfig is diffrent from id_wordconfig stored in hiddenwords table.
For example wordconfigs table cointains:
id_wordconfig = 1 (this row is related to successively: id_account=1
(imporatant that 1), id_language=1, id_category=1)
id_wordconfig = 2 (this row is related to successively: id_account=1
(imporatant that 1), id_language=1, id_category=1)
I pass parameters to query:
-id_category = 1
-id_language = 1
-id_account = 2 (other than assigned to wordconfigs in example)
Example 1
hiddenwords table is empty -> query returns wordconfid with id 1 and 2
Example 2
hiddenwords table contains:
id_hiddenwords = 1; id_wordconfig=1; id_account=1
query returns also wordconfig with id 1 and 2 (because id_account is diffrent from passed to query)
Example 3
hiddenwords table contains:
id_hiddenwords = 1; id_wordconfig=1; id_account=2
query returns just wordconfig with id 2 (because hiddenwords contains id_wordconfig=1 and id_account=2 like passed to query)
Example 4
hiddenwords table contains:
id_hiddenwords = 1; id_wordconfig=1; id_account=2
id_hiddenwords = 1; id_wordconfig=2; id_account=1 (different that passed to query)
query returns just wordconfig with id = 2
Example 5
hiddenwords table contains:
id_hiddenwords = 1; id_wordconfig=1; id_account=2
id_hiddenwords = 1; id_wordconfig=2; id_account=2
query should nothing returns
I've created a simple query for this task:
SELECT wc.*
FROM categorywordconfig cwc, categories c, languages l, accounts a, wordconfigs wc
LEFT JOIN hiddenwords hw ON hw.id_wordconfig<>wc.id_wordconfig
WHERE wc.id_wordconfig=cwc.id_wordconfig
AND cwc.id_category=c.id_category
AND c.id_language=l.id_language
AND l.id_account=a.id_account
AND c.id_category=1
AND l.id_language=1
AND hw.id_account=2
The given query works for examples from 1 to 4. The Example 5 should nothing returns but with this query it's return all rows (so wordconfig with id 1 and 2) Why is that? What's wrong with it?
Edit:
I've just simple changed query to:
SELECT wc.* FROM categorywordconfig cwc, categories c, languages l, accounts a, wordconfigs wc
WHERE wc.id_wordconfig=cwc.id_wordconfig
AND cwc.id_category=c.id_category AND c.id_language=l.id_language
AND l.id_account=a.id_account AND c.id_category=1 AND l.id_language=1
AND wc.id_wordconfig NOT IN (SELECT id_wordconfig FROM hiddenwords WHERE id_account=2)
And it's working :)
Related
hi i have two query result:
Table A
Id u
1 50,00
2 60,00
3 70,00
and
Table B
id c
4 110,00
5 120,01
6 130,02
Now i have doing two query on this table and i want sum their query result.
I want update column c from table B with 160 that is sum of(110+50).
Table B
Id c
4 160,00
Table B and Table A they have nothing in common.
Now i have doing two query for select their value ed for sum two data:
$data=number_format($row['c']+$row1['u']);
$query_updatee="update B set c= (integer)$data where c=110,00";
Can i sum the data from two different table that don't have nothing in comon?
My output is pg_query(): Query failed: ERRORE: syntax error at or near "1" LINE 1: update B set punti = (integer)1680,00 where c=110,00 ^ in C:\xampp\htdocs\table_A.php on line 81
You can use a subquery to fetch the increment. The rest seems to just be filtering:
update B
set c = c + (select a.u from a where a.id = 1)
where B.id = 4;
Obviously, you can use where b.c = 110.00 if you want to filter by a number (or use a comma if that is how the database is set up).
I have a table:
id name
1 A
3 B
2 C
4 D
I am trying to select a few rows using the following query in a stored procedure:
select * from test where id = all ('{1,2}');
I am expecting it to return rows with ids 1 and 2, however, it returns empty.
The following query works as expected:
select * from test where id <> all ('{3,4}');
It is returning rows with id 1 and 2. I am unable to understand why '=' operator is not working as expected but '<>' is working. I am new to this syntax. Please help how to get the expected result that is using equal operator (equivalent to IN).
I am expecting it to return rows with ids 1 and 2
You want any, not all:
where id = any ('{1,2}');
This brings id that are either equal to 1 or 2. In other words that's equivalent to id in (1, 2).
As regard to this expression:
where id <> all ('{3,4}');
This is equivalent to:
where not (id = any ('{3,4}'));
So this filters out ids 3 and 4.
Concerning your original expression:
where id = all ('{1,2}');
This does not make sense; a single value cannot be matched against all values of an array at once - so this filters out all rows.
I have a set of data that contains 2 sets of identifiers: a unique number for that record, Widget_Number, and the original unique number for the record, Original_Widget_Number. Typically these two values are identical but when a record has been revised, the a new record is created with a new Widget_Number, preserving the old Widget_Number value in Original_Widget_Number. IE SELECT * FROM widgets WHERE Widget_Number != Original_Widget_Number returns all records that have been changed. (Widget_Number increments by 10 for new widgets and by 1 for revised widgets.)
I would like to return all records that were changed as well as the original records related to those records. For example if I had a table containing:
Widget_Number Original_Widget Number More_Data
1: 10 10 Stephen
2: 11 10 Steven
3: 20 20 Joe
I would like a query to return rows 1 & 2. I know I could loop trough this in a higher-level language but is there a straightforward way to do this in MS SQL?
using exists():
select *
from widgets as t
where exists (
select 1
from widgets as i
where i.original_widget_number = t.original_widget_number
and i.widget_number != i.original_widget_number
)
or in()
select *
from widgets as t
where t.original_widget_number in (
select i.original_widget_number
from widgets as i
where i.widget_number != i.original_widget_number
)
The following should get both the records that have changed and the original records:
select w.*
from widgets w
where w.widget_number <> w.original_widget_number or
exists (select 1
from widgets w2
where w.widget_number = w2.original_widget_number and
w2.widget_number <> w2.original_widget_number
);
select * from widget
where original_widget_number in
(select original_widget_number from widget
where widget_number <> original_widget_number)
We're dealing with geographic data with our Oracle database.
There's a function called ST_Insertects(x,y) which returns true if record x intersects y.
What we're trying to do is, compare each record of table A with all records of table B, and check two conditions
condition 1 : A.TIMEZONE = 1 (Timezone field is not unique)
condition 2 : B.TIMEZONE = 1
condition 3 : ST_Intersects(A.SHAPE, B.SHAPE) (Shape field is where the geographical information is stored)
The result we're looking for is records ONLY from the table A that satisfy all 3 conditions above
We tried this in a single select statement but it doesn't seem to make much sense logically
pseudo-code that demonstrates a cross-join:
select A.*
from
tbl1 A, tbl2 B
where
A.TIMEZONE = 1 and
B.TIMEZONE = 1 and
ST_Intersects(A.SHAPE, B.SHAPE)
if you get multiples, you can put a distinct and only select A.XXX columns
With a cross-join rows are matched like this
a.row1 - b.row1
a.row1 - b.row2
a.row1 - b.row3
a.row2 - b.row1
a.row2 - b.row2
a.row2 - b.row3
So if row 1 evaluates to true on multiple rows, then just add a distinct on a.Column1, etc.
If you want to use the return value from your function in an Oracle SQL statement, you will need to change the function to return 0 or 1 (or 'T'/'F' - some data type supported by Oracle Database, which does NOT support the Boolean data type).
Then you probably want something like
select <columns from A>
from A
where A.timezone = 1
and exists ( select *
from B
where B.timezone = 1
and ST_intersects(A.shape, B.shape) = 1
)
this is a sample sql query that i created ms access query. i am trying to get only one row the min(DATE). how ever when i run my query i get multiple lines. any hits? thanks
SELECT tblWarehouseItem.whiItemName,
tblWarehouseItem.whiQty,
tblWarehouseItem.whiPrice,
Min(tblWarehouseItem.whiDateIn) AS MinOfwhiDateIn,
tblWarehouseItem.whiExpiryDate,
tblWarehouseItem.whiwrhID
FROM tblWarehouseItem
GROUP BY tblWarehouseItem.whiDateIn,
tblWarehouseItem.whiItemName,
tblWarehouseItem.whiQty,
tblWarehouseItem.whiPrice,
tblWarehouseItem.whiExpiryDate,
tblWarehouseItem.whiwrhID;
If i have my sql code like that is working as it should:
SELECT MIN(tblWarehouseItem.whiDateIn) FROM tblWarehouseItem;
In the first query, you group by a number of columns. That means the minimum value will be calculated for each group, which in turn means you may have multiple rows. On the other hand, the second query will only get the minimum value for the specified column from all rows, so that there is only one row in the result set.
A simple example is shown below to illustrate the above.
Table:
Key Value
1 1
1 2
2 3
2 4
On Group By Key:
GroupKey MinValue
1 = min(1,2) = 1 -> Row 1
2 = min(3,4) = 3 -> Row 2
On Min (Value)
MinValue
=min(1,2,3,4) = 1 -> Row 1
For a table like above, if you want to select all rows and also show the minimum value from whole table rather than per group, you can do something like this:
select key, (select min(value) from table)
from table
SELECT WI.*
FROM tblWarehouseItem AS WI INNER JOIN (SELECT whiimtID, MIN(tblWarehouseItem.whiDateIn) AS whiDateIn
FROM tblWarehouseItem
GROUP BY whiimtID) AS MinWI ON (WI.whiDateIn = MinWI.whiDateIn) AND (WI.whiimtID = MinWI.whiimtID);