exists

Check if record exists from controller in Rails

两盒软妹~` 提交于 2019-11-26 12:35:17
问题 In my app a User can create a Business. When they trigger the index action in my BusinessesController I want to check if a Business is related to the current_user.id : If yes: display the business. If no: redirect to the new action. I was trying to use this: if Business.where(:user_id => current_user.id) == nil # no business found end But it always returns true even when the business doesn\'t exist... How can I test if a record exists in my database? 回答1: Why your code does not work? The

MySQL > Table doesn't exist. But it does (or it should)

拜拜、爱过 提交于 2019-11-26 11:30:45
I did change the datadir of a MySQL installation and following some steps it worked fine. Every base I had was moved correctly but one. I can connect and USE the database, even SHOW TABLES returns me all the tables correctly and the files of each table exists on the mysql data directory. But when I try to SELECT something there, it says the table doesn't exists. But the table does exists, it even shows at SHOW TABLES statement! My guess is that the SHOW TABLES lists the files existence somehow that the files are corrupt or something like that but it doesn't check it. So I can list them but not

How to check if mysql database exists

喜欢而已 提交于 2019-11-26 11:03:44
Is it possible to check if a (MySQL) database exists after having made a connection. I know how to check if a table exists in a DB, but I need to check if the DB exists. If not I have to call another piece of code to create it and populate it. I know this all sounds somewhat inelegant - this is a quick and dirty app. Kirtan SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'DBName' If you just need to know if a db exists so you won't get an error when you try to create it, simply use (From here ): CREATE DATABASE IF NOT EXISTS DBName; A simple way to check if a database

PL/pgSQL checking if a row exists

久未见 提交于 2019-11-26 09:02:21
问题 I\'m writing a function in PL/pgSQL, and I\'m looking for the simplest way to check if a row exists. Right now I\'m SELECTing an integer into a boolean , which doesn\'t really work. I\'m not experienced with PL/pgSQL enough yet to know the best way of doing this. Here\'s part of my function: DECLARE person_exists boolean; BEGIN person_exists := FALSE; SELECT \"person_id\" INTO person_exists FROM \"people\" p WHERE p.person_id = my_person_id LIMIT 1; IF person_exists THEN -- Do something END

What is easier to read in EXISTS subqueries? [closed]

你说的曾经没有我的故事 提交于 2019-11-26 08:53:32
问题 It\'s a question of readability . There is no difference in performance. Old versions of SQL Server were silly enough to look up meta data, but not any more. SELECT foo FROM bar WHERE EXISTS (SELECT * FROM baz WHERE baz.id = bar.id); SELECT foo FROM bar WHERE EXISTS (SELECT 1 FROM baz WHERE baz.id = bar.id); I am not considering NULL or \"fun variants\" which don\'t seem intuitive to me. SELECT foo FROM bar WHERE EXISTS (SELECT NULL FROM baz WHERE baz.id = bar.id); SELECT foo FROM bar WHERE

Linq select objects in list where exists IN (A,B,C)

只谈情不闲聊 提交于 2019-11-26 08:01:50
问题 I have a list of orders . I want to select orders based on a set of order statuses. So essentially select orders where order.StatusCode in (\"A\", \"B\", \"C\") // Filter the orders based on the order status var filteredOrders = from order in orders.Order where order.StatusCode.????????(\"A\", \"B\", \"C\") select order; 回答1: Your status-codes are also a collection, so use Contains: var allowedStatus = new[]{ "A", "B", "C" }; var filteredOrders = orders.Order.Where(o => allowedStatus.Contains

MongoDB: How to query for records where field is null or not set?

余生长醉 提交于 2019-11-26 07:24:20
问题 I have an Email document which has a sent_at date field: { \'sent_at\': Date( 1336776254000 ) } If this Email has not been sent, the sent_at field is either null, or non-existant. I need to get the count of all sent/unsent Emails . I\'m stuck at trying to figure out the right way to query for this information. I think this is the right way to get the sent count: db.emails.count({sent_at: {$ne: null}}) But how should I get the count of the ones that aren\'t sent? 回答1: If the sent_at field is

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS

邮差的信 提交于 2019-11-26 04:46:32
问题 My query is as follows, and contains a subquery within it: select count(distinct dNum) from myDB.dbo.AQ where A_ID in (SELECT DISTINCT TOP (0.1) PERCENT A_ID, COUNT(DISTINCT dNum) AS ud FROM myDB.dbo.AQ WHERE M > 1 and B = 0 GROUP BY A_ID ORDER BY ud DESC) The error I am receiving is ... Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.` When I run the sub-query alone, it returns just fine, so I am assuming there is some issue with the

Best way to check if object exists in Entity Framework?

泪湿孤枕 提交于 2019-11-26 04:21:17
问题 What is the best way to check if an object exists in the database from a performance point of view? I\'m using Entity Framework 1.0 (ASP.NET 3.5 SP1). 回答1: If you don't want to execute SQL directly, the best way is to use Any(). This is because Any() will return as soon as it finds a match. Another option is Count(), but this might need to check every row before returning. Here's an example of how to use it: if (context.MyEntity.Any(o => o.Id == idToMatch)) { // Match! } And in vb.net If

MySQL > Table doesn't exist. But it does (or it should)

こ雲淡風輕ζ 提交于 2019-11-26 03:32:34
问题 I changed the datadir of a MySQL installation and all the bases moved correctly except for one. I can connect and USE the database. SHOW TABLES also returns me all the tables correctly, and the files of each table exists on the MySQL data directory. However, when I try to SELECT something from the table, I get an error message that the table does not exist. Yet, this does not make sense since I was able to show the same table through SHOW TABLES statement. My guess is that SHOW TABLES lists