I try something like
SELECT * FROM table WHERE aspect_id = (SELECT floor(random(aspect_id)::int FROM generate_series(1,8));
and I want to get several aspect_id from table.
Or I try to
SELECT * FROM table WHERE aspect_id = SELECT random_between(1,100) FROM generate_series(1,5);
But still nothing
Could you help me?
What about
SELECT * FROM atable
WHERE aspect_id BETWEEN 1 AND 100
ORDER BY random()
LIMIT 5;
Related
I am passing a simple query where I am searching for specific rows where OrderID is an even number
SELECT *
FROM Orders
WHERE mod(OrderID,2) = 0;
Error :
Syntax error (missing operator) in query expression 'mod(OrderID,2) = 0'.
You are not using Oracle, so you should be using the modulus operator:
SELECT * FROM Orders where OrderID % 2 = 0;
The MOD() function exists in Oracle, which is the source of your confusion.
Have a look at this SO question which discusses your problem.
SELECT * FROM Orders where OrderID % 2 = 0;///this is for even numbers
SELECT * FROM Orders where OrderID % 2 != 0;///this is for odd numbers
Sql Server we can use %
select * from orders where ID % 2 = 0;
This can be used in both Mysql and oracle.
It is more affection to use mod function that %.
select * from orders where mod(ID,2) = 0
MOD() function exists in both Oracle and MySQL, but not in SQL Server.
In SQL Server, try this:
SELECT * FROM Orders where OrderID % 2 = 0;
--This is for oracle
SELECT DISTINCT City FROM Station WHERE MOD(Id,2) = 0 ORDER BY City;
Try this:
SELECT DISTINCT city FROM STATION WHERE ID%2=0 ORDER BY CITY;
For SQL Server:
SELECT * FROM Orders where OrderID % 2 = 0; //this is for even numbers
SELECT * FROM Orders where OrderID % 2 != 0; //this is for odd numbers
For Oracle and MySQL, you have to use the MOD function:
select * from orders where mod(ID,2) = 0
SELECT * FROM ( SELECT *, Row_Number()
OVER(ORDER BY country_gid) AS sdfg FROM eka_mst_tcountry ) t
WHERE t.country_gid % 2 = 0
In SQL, all these options can be used for MOD:
SELECT * FROM CITY WHERE MOD(ID,2) = 0 ORDER BY CITY;
OR
SELECT * FROM CITY WHERE ID % 2 = 0 ORDER BY CITY;
OR
SELECT * FROM CITY WHERE ID MOD 2 = 0 ORDER BY CITY;
Hope it helps!
hey guys this is a very simple sql query that is not giving me the correct result.
subquery:
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
this subquery successfully returns a correct value 627809
simple query:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (627809)
this query executes properly and returns 4 rows.(4 addresses for a member)
but if I try to combine these queries in 1 query as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
then the query returns 0 rows. why is this happening?
Thanks
Your query looks OK, the only I can think is maybe you mistake the value on the result.
can you try this:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
and this
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
)
Since your Order (presumably) can only carry a single Member_ID -- can you please try your full query without the "IN", rather try an equal join as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID = (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
I need to select records that has ID = 10,23,30 so I wrote this SQL
Select * from mytable where position(id in '10,23,30') > 0
But the problem I get additional records where ID = 1 and 2
Any ideas how to select only what I need ?
No need for position, just do IN:
Select * from mytable
where id in (10,23,30)
Use IN operator:
Select * from mytable where id in (10,23,30)
I'm trying to select a string value based on a sub-query that returns another string value.
While cases like
select * from ForgeRock where (SELECT count(*) from...) = 1;
select * from ForgeRock where (SELECT num1 from...) = num2;
work, something like this returns 0 lines:
select * from ForgeRock where (SELECT name from...) = 'OpenDJ';
Please note: I'm talking about cases where
SELECT name from...
SELECT num1 from...
return exactly 1 row. The following don't help either:
select * from ForgeRock where (SELECT min(name) from...) = 'OpenDJ';
select * from ForgeRock where (SELECT top 1 name from...) = 'OpenDJ';
Here is a fiddle link that demonstrates the problem with strings:
http://sqlfiddle.com/#!6/a7540/1711
Here is another fiddle link that proves there's no problem with numbers:
http://sqlfiddle.com/#!6/a7540/1715
So...how can I manage to grab those sub-query strings?
Look at this:
(SELECT 'productName') = 'OpenDJ'
it does the same as:
'productName' = 'OpenDJ'
That will never be true.
This will give you a result:
select * from ForgeRock where productName = (select 'OpenDJ');
I have a sql query that I run against a sql server database eg.
SELECT * FROM MyTable WHERE Id = 2
This may return a number of records or may return none. If it returns none, I would like to alter my sql query to return a default record, is this possible and if so, how? If records are returned, the default record should not be returned. I cannot update the data so will need to alter the sql query for this.
Another way (you would get an empty initial rowset returned);
SELECT * FROM MyTable WHERE Id = 2
IF (##ROWCOUNT = 0)
SELECT ...
SELECT TOP 1 * FROM (
SELECT ID,1 as Flag FROM MyTable WHERE Id = 2
UNION ALL
SELECT 1,2
) qry
ORDER BY qry.Flag ASC
You can have a look to this post. It is similar to what you are asking
Return a value if no rows are found SQL
I hope that it can guide you to the correct path.
if not exists (SELECT top 1 * FROM mytable WHERE id = 2)
select * from mytable where id= 'whatever_the_default_id_is'
else
select * from mytable where id = 2
If you have to return whole rows of data (and not just a single column) and you have to create a single SQL query then do this:
Left join actual table to defaults single-row table
select
coalesce(a.col1, d.col1) as col1,
coalesce(a.col2, d.col2) as col2,
...
from (
-- your defaults record
select
default1 as col1,
default2 as col2,
...) as d
left join actual as a
on ((1 = 1) /* or any actual table "where" conditions */)
The query need to return the same number of fields, so you shouldn't do a SELECT * FROM but a SELECT value FROM if you want to return a default value.
With that in mind
SELECT value FROM MyTable WHERE Id = 2
UNION
SELECT CASE (SELECT count(*) FROM MyTable WHERE Id = 2)
WHEN 0 THEN 'defaultvalue'
END