Sorry to pester with a simple problem but I'm stumped with a simple select on an HTML5 WebSQL Data Base.
Table tPhones has id, hid, location and several other columns. I would like to return a list of rows with where hid = [input value] or, if no rows with that hid exist, return rows where hid = 1.
I have tried LIMIT 1 ACS etc but that too fails.
Function showPhones(){
var phonegroup = $(this).attr("id");
var hid = localStorage.hid;
db.transaction (function(transaction)
{
var sql = "SELECT * FROM tPhones WHERE dept ='"+phonegroup+"' AND ((hid ='"+hid+"') OR IFNULL (hid ='1')) ORDER BY location ASC";
transaction.executeSql (sql, undefined,function(transaction,result)
{…………..rest of function working fine
Any help would be much appreciated.
A normal expression in the WHERE clause can access only columns of the current record.
To check for the existence of any other record, you have to use a subquery:
SELECT *
FROM tPhones
WHERE dept = ?
AND (hid = ? OR (hid = 1 AND
NOT EXISTS (SELECT 1
FROM tPhones
WHERE dept = ?
AND hid = ?)))
ORDER BY location
Related
ALTER PROC [dbo].[rendertutorials](#courname nvarchar(30))
AS
BEGIN
SELECT
tu.FkCategoryid AS Fcatid
,tu.Title
,tu.content
,tu.Keywords
,tu.Metadata
,tu.tags
,cat.CategoryName
,cat.CourseName
,cat.courseimagepath AS imgpath
FROM GetCategories AS cat
INNER JOIN GetTutorials AS tu ON cat.CategoryId = tu.FkCategoryId
WHERE tu.publish = 1 AND cat.coursename LIKE '%' + #courname + '%'
END
GO
EXEC[rendertutorials] "mvc"
AS you can see the store procedure will produce the correct data. item[0] ... item[n] are different.
public IActionResult Tutorials()
{
//var data = GetTableLastChanges("listoftitiles #title", 1, "#title");
var data = db.getrenderview.FromSqlRaw("rendertutorials #courname={0}", "mvc").ToList();
return View(data);
}
But when I call the stored procedure from code the above data variable contain the same data (from item[0] till item[n]). Why? What did I miss?
use distinct in sql:
SELECT DISTINCT column1, column2, ...
FROM table_name;
I am trying to run a query / select statement and save it in a variable. I know how to get something specific from a specific column but not from counting rows.
This is working as I getting MYID specifically.
ResultSet MYIDrs = stmtCFG.executeQuery( "SELECT rowid, MYID from MYINDEX order by rowid desc limit 1;" );
MYID = MYIDrs.getString("MYID");
Now I am trying to count the rows that works in SQLite client but not in the jdbc as I can't figure out what to request.
this is what I have but is not resulting in what I am expecting.
ResultSet FILE_COUNTrs = stmtCFG.executeQuery( "SELECT count(*) from TABLE where MYID = '"+MYID+"';");
FILE_COUNT = FILE_COUNTrs.getString(?????);
problem or question is: What do I put in the ????? as I already tried everything.
I am expecting to see a number.
I am really sorry I found what I was looking for by assigning a name TOTAL
This is my code and it works...
ResultSet FILE_COUNTrs = stmtCFG.executeQuery( "SELECT count(*) AS TOTAL from TABLE where MYID = '"+MYID+"';");
FILE_COUNT = FILE_COUNTrs.getString("TOTAL");
You use wrong data type. COUNT(*) returns Integer type, Not String.
You can do like this without assigning a label for COUNT(*)
int FILE_COUNT = FILE_COUNTrs.getInt(1); // 1: is the column index of COUNT(*)
I tried :
UPDATE closure JOIN item ON ( item_id = id )
SET checked = 0
WHERE ancestor_id = 1
And:
UPDATE closure, item
SET checked = 0
WHERE ancestor_id = 1 AND item_id = id
Both works with MySQL, but those give me a syntax error in SQLite.
How can I make this UPDATE / JOIN works with SQLite version 3.5.9 ?
You can't. SQLite doesn't support JOINs in UPDATE statements.
But, you can probably do this with a subquery instead:
UPDATE closure SET checked = 0
WHERE item_id IN (SELECT id FROM item WHERE ancestor_id = 1);
Or something like that; it's not clear exactly what your schema is.
You can also use REPLACE then you can use selection with joins.
Like this:
REPLACE INTO closure
SELECT sel.col1,sel.col2,....,sel.checked --checked should correspond to column that you want to change
FROM (
SELECT *,0 as checked FROM closure LEFT JOIN item ON (item_id = id)
WHERE ancestor_id = 1) sel
I got about 5 look-a-like linq querys just like this SortPerson() metod. I'm trying to develop a search using dropdowns where a user can select values from the dropdown and returns the values that are true from one or more dropdowns the user has selected to use.
Is there a simpler way to develop this? help would be much appreciated
public void SortPerson()
{
var personId = ddlPerson.SelectedValue;
var data = new MyModelContext();
var documents = from d in data.tblDocuments
join sp in data.tblPersons on d.DocPerson equals sp.PersonId
select d;
if (!String.IsNullOrEmpty(personId))
{
documents = documents.Where(c => c.DocPerson.Equals(personId));
}
rptResult.DataSource = documents.ToList();
rptResult.DataBind();
}
I don't see the point in joining without Where if you still select only one table.
If you want all document in case when Person is not selected, then you can't create much simpler method. You can write it shorter like:
var documents =
from d in data.tblDocuments
join ...
where String.IsNullOrEmpty(personId) || d.DocPerson equals personId
select d;
so you don't need separate if statement.
If you want to use several values from 5 dropdowns and use them as conditions in single query, just add more conditions:
var personId = ddlPerson.SelectedValue;
var someValue = ddlSomeDDL.SelectedValue;
//3 more values from DDL
var documents = from d in data.tblDocuments
join sp in data.tblPersons on d.DocPerson equals sp.PersonId
where (String.IsNullOrEmpty(personId) || sp.PersonId equals personId)
&& (String.IsNullOrEmpty(someValue) || d.SomeColumn equals someValue)
//3 more conditions
select d;
I tried :
UPDATE closure JOIN item ON ( item_id = id )
SET checked = 0
WHERE ancestor_id = 1
And:
UPDATE closure, item
SET checked = 0
WHERE ancestor_id = 1 AND item_id = id
Both works with MySQL, but those give me a syntax error in SQLite.
How can I make this UPDATE / JOIN works with SQLite version 3.5.9 ?
You can't. SQLite doesn't support JOINs in UPDATE statements.
But, you can probably do this with a subquery instead:
UPDATE closure SET checked = 0
WHERE item_id IN (SELECT id FROM item WHERE ancestor_id = 1);
Or something like that; it's not clear exactly what your schema is.
You can also use REPLACE then you can use selection with joins.
Like this:
REPLACE INTO closure
SELECT sel.col1,sel.col2,....,sel.checked --checked should correspond to column that you want to change
FROM (
SELECT *,0 as checked FROM closure LEFT JOIN item ON (item_id = id)
WHERE ancestor_id = 1) sel