I am performing a query to a database in ruby using Sequel ORM. The query is performed by:
query = "SELECT * FROM albums WHERE artist = 'John';"
DB.fetch(query)
I would like to check whether the result of the query is empty, i.e. if not entry of the DB matches the query conditions.
I could:
empty = true
DB.fetch(query) do |row|
empty = false
end
but I would like to know whether there is a direct method to chek whether the query returns no result.
You want the empty? method:
query = "SELECT * FROM albums WHERE artist = 'John';"
empty = DB.fetch(query).empty?
Related
Below is an SQL query which fetches some data related to user.
def self.get_user_details(user_id)
result = Event.execute_sql("select replace(substring_index(properties, 'text', -1),'}','') as p, count(*) as count
from ahoy_events e where e.user_id = ?
group by p order by count desc limit 5", :user_id)
return result
end
I want to dynamically pass values to user id to get the result.
I am using the below method to sanitize sql array, but still it returns no result. The query works fine if given static parameter.
def self.execute_sql(*sql_array)
connection.execute(send(:sanitize_sql_array, sql_array))
end
Because the query is complicated I am couldn't figure out the ActiveRecord way to get the results.
Is there any way I could get this sorted out?
I don't know why that should not work. May SQL-Syntac SELECT * FROM users ...
dynamic passing some other way you can do this: "some string #{user.id#ruby code} some more string"
You can do querys in Activerecord like this: User.where(id: user.id).where(field2: value2).group_by(:somevalue).order(:id)
Okay so, this is my query.
select id from rooms where owner = 'oknow';
and the answer I get is
325
However, I made another SQL within this one as below
update users set home_room = 'mysql_fetch_assoc()' where username = 'omarisgod';
I want the 'mysql_fetchassoc()' to be the '325' value, how do I do this?
A subquery will do this:
UPDATE users SET home_room = (SELECT id FROM rooms WHERE owner = 'oknow') WHERE username = 'omarisgod';
You can conceptualize it thusly: The query inside parentheses will return a result, which will be utilized by the outer query.
I have the following database scheme on MySQL and I would like to retrieve all elements for a speciic id.
So for instance, I would like to retrieve cities, categories, departments linked to the coupon_id=1 (and other fields).
I wrote the following SQL query but unfortunatelly could not get the desired result.
SELECT cc_coupon.id_coupon as idCoupon,
cc_coupon.condition_coupon,
cc_coupon.description,
cc_coupon.type_coupon,
cc_coupon_by_categorie.id_categorie,
cc_categorie.categorie as category,
cc_annonceur.raison_sociale,
cc_coupon_active_in_cities.id_ville as ville_slug,
cc_villes_france.ville_slug,
cc_villes_france.ville_nom_departement,
cc_villes_france.ville_departement
FROM cc_coupon,
cc_coupon_by_categorie,
cc_categorie,
cc_annonceur,
cc_coupon_active_in_cities,
cc_coupon_active_in_departments,
cc_villes_france
WHERE cc_coupon.id_coupon = cc_coupon_by_categorie.id_coupon
and cc_categorie.id_categorie = cc_coupon_by_categorie.id_categorie
and cc_coupon.id_annonceur = cc_annonceur.id_annonceur
and cc_coupon.id_coupon = cc_coupon_active_in_cities.id_coupon
and cc_villes_france.id_ville = cc_coupon_active_in_cities.id_ville
and cc_villes_france.ville_departement = cc_coupon_active_in_departments.ville_departement
and cc_coupon.id_coupon = 1
and cc_coupon_active_in_cities.id_coupon = 1
and cc_coupon_active_in_departments.id_coupon = 1
Thanks for your help.
I think you should use the on and not where when you want to join two tables. When you want to specify other conditions use where clause.
I have a piece of code which fetches the list of ids of users I follow.
#followed = current_user.followed_user_ids
It gives me a result like this [11,3,24,42]
I need to add these to NOT IN mysql query.
Currently, I am getting NOT IN ([11,3,24,42]) which is throwing an error. I need NOT IN (11,3,24,42)
This is a part of a find_by_sql statement, so using where.not is not possible for me in this point.
In rails 4:
#followed = current_user.followed_user_ids # #followed = [11,3,24,42]
#not_followed = User.where.not(id: #followed)
This should generate something like select * from users where id not in (11,3,24,42)
As you comment, you are using find_by_slq (and that is available in all rails versions). Then you could use the join method:
query = "select * from users where id not in (#{#followed.join(',')})"
This would raise mysql errors if #followed is blank, the resulting query would be
select * from users where id not in ()
To solve this whiout specifiying aditional if statements to your code, you can use:
query = "select * from users where id not in (0#{#followed.join(',')})"
Your normal queries would be like:
select * from users where id not in (01,2,3,4)
but if the array is blank then would result in
select * from users where id not in (0)
which is a still valid sql statement and is delivering no results (which might be the expected situation in your scenario).
you can do something like:
#followed = [11,3,24,42]
User.where('id not in (?)', #followed)
I am very frustrated from linq to sql when dealing with many to many relationship with the skip extension. It doesn't allow me to use joinned queries. Not sure it is the case for SQL server 2005 but I am currently using SQL Server 2000.
Now I consider to write a store procedure to fetch a table that is matched by two tables e.g. Album_Photo (Album->Album_Photo<-Photo) and Photo table and only want the Photos data so I match the Album's ID with Album_Photo and use that ID to match the photo. In the store procedure I am just fetch all the joinned data. After that in the linq to sql, I create a new Album object.
e.g.
var albums = (from r in result
where (modifier_id == r.ModifierID || user_id == r.UserID)
select new Album() {
Name = r.Name,
UserID = r.UserID,
ModifierID = r.ModifierID,
ID = r.ID,
DateCreated = r.DateCreated,
Description = r.Description,
Filename = r.Filename
}).AsQueryable();
I used the AsQueryable to get the result as a IQueryable rather than IEnumerable. Later I want to do something with the collection, it gives me this error:
System.InvalidOperationException: The query results cannot be enumerated more than once.
It sounds like you have a situation where the query has already executed by the time you are want to filter it later in your code.
Can you do something like...
var albums = (blah blah blah).AsQueryable().Where(filterClause) when you have enough info to process
what happens if you try albums.where(filter) later on in the code? Is this what you are trying?