I've a problem with the following function. Its just the join I think. By using sql statement "SELECT * FROM pkw", I got the results. Both tables are filled with data.
Do you see the mistake?
function gotoauftrag(id) {
db = window.sqlitePlugin.openDatabase(shortName, version, displayName,maxSize);
var list = '';
var sql = "SELECT DISTINCT p.name FROM pkw p INNER JOIN ta_pkw tp ON tp.pkw_id = p.pkw_id AND tp.ta_id = ?";
db.transaction(function (tx) {
tx.executeSql(sql, [id], function (tx, results) {
if (results.rows.length > 0) {
document.getElementById('auftrag_pkw').innerHTML = '';
for (i = 0; i < results.rows.length; i++) {
r = results.rows.item(i);
list = '<li>'+r['name']+'<span class="ui-li-count">1</span></li>';
document.getElementById('auftrag_pkw').innerHTML += list;
$('#auftrag_pkw').listview('refresh');
}
}
});
});
$('#auftrag_pkw').listview('refresh');
}
I don't believe you are able to bind to the JOIN predicate. Try using the WHERE clause instead.
SELECT DISTINCT
p.name
FROM pkw p
INNER JOIN ta_pkw tp
ON tp.pkw_id = p.pkw_id
WHERE
tp.ta_id = ?
Also, you probably should be doing the ta_id selection before your join. The above will do a natural join on pkw_id, THEN it will filter the results based on ta_id.
Related
I implement the join query for join two tables. How to return those values.
public List<cartItem> getcartItem(int userId)
{
var data = (from c in _context.cartItems join p in _context.Products
on c.productId equals p.ProId
where c.userId == userId && c.status==false select new {
proName=p.ProductName,
qty=c.quantity,
proImg=p.ImageUrl,
uPricce=p.price
}).ToList();
return data;
}
Assuming these are the properties of a cartItem obj, you just need to assign the select statement to match the return type and new up a cartItem.
...select new cartItem
{
proName = p.ProductName,
qty = c.quantity,
proImg = p.ImageUrl,
uPricce = p.price
}).ToList();
var list = db.SomeModels.FromSql("select id from table1 where rownum < :r",10).ToList();
gives me
System.ArgumentException: 'Invalid parameter binding Parameter name: r'
What am I missing. Can't find any docs on that
The following should work:
var row = new OracleParameter("r", OracleDbType.Int32);
row.Value = 10;
var list = db.SomeModels
.FromSql("select id from table1 where rownum < :r", new object[] { row })
.ToList();
To add multiple parameters, add more OracleParameter objects to the object array:
var row = new OracleParameter("r", OracleDbType.Int32);
row.Value = 10;
var nameParameter = new OracleParameter("name", OracleDbType.Varchar2);
nameParameter.Value = "John";
var list = db.SomeModels
.FromSql("select id from table1 where rownum < :r and firstname = :name", new object[] { row, nameParameter })
.ToList();
Unfortunately, like yourself I haven't found any proper documentation for it. I will edit the answer when I do.
The following code will also work (tested with Oracle.ManagedDataAccess.Core v2.19.31 and Microsoft.EntityFrameworkCore v2.2.6):
var list = db.SomeModels
.FromSql("select id from table1 where rownum < {0}", 10)
.ToList()
FMDB seems to have lots of functions to deal with columns but none for rows. All I am trying to do is find the number of rows in a table.
func getTableRows() {
sharedInstance.database!.open()
let sqlStatement = "SELECT COUNT(*) FROM NAMESTABLE"
var resultSet: FMResultSet! = sharedInstance.database!.executeQuery(sqlStatement, withArgumentsInArray: nil)
if (resultSet != nil) {
let rowCount = XXXX
NSLog("Table Rows = %i",rowCount)
}
sharedInstance.database!.close()
}
I have tried various approaches with the XXXX but no joy. Am I going about this the wrong way? Many thanks.
try this:
func getTableRows() {
sharedInstance.database!.open()
let sqlStatement = "SELECT COUNT(field) FROM table_name"
var resultSet: FMResultSet! = sharedInstance.database!.executeQuery(sqlStatement, withArgumentsInArray: nil)
if (resultSet != nil) {
let rowCount = XXXX
NSLog("Table Rows = %i",rowCount)
}
sharedInstance.database!.close()
}
Please correct the query
IN PL/SQL
SELECT a.MENU_ID, a.menu_label, a.menu_value
FROM tbl_ims_menu a, TBL_IMS_ROLE_ASSIGNED_MENU b,TBL_IMS_USER_ROLE_PRIVILEGES c
WHERE a.menu_id = b.menu_id AND b.urole_id = c.granted_role
AND c.user_id = '3' AND a.menu_master <> '0'
AND a.menu_status = 'Active'
GROUP BY a.menu_id, a.menu_label, a.menu_value
query is working fine there is some issue when rewrite in Entity framework
check the following query
List<TBL_IMS_MENU> listSubMenu = (from m in db.TBL_IMS_MENU
join ra in db.TBL_IMS_ROLE_ASSIGNED_MENU on m.MENU_ID
equals ra.MENU_ID
join rp in db.TBL_IMS_USER_ROLE_PRIVILEGES on ra.UROLE_ID
equals rp.GRANTED_ROLE
where rp.USER_ID == UserID
group m by m.MENU_ID
into g select g).ToList();
if I used Var instead of List then how to fire loop?
I think you need to remove your join statements - and just use the where like you do in raw SQL query:
var qry = (from a in db.TBL_IMS_MENU
from b in db.TBL_IMS_ROLE_ASSIGNED_MENU
from c in db.TBL_IMS_USER_ROLE_PRIVILEGES
where c.USER_ID == UserID
where b.UROLE_ID == c.GRANTED_ROLE
where a.MENU_ID == b.MENU_ID
where a.menu_status == "Active"
where a.menu_master != "0"
select a)
.GroupBy(c => c.menu_id)
.ThenBy(c => c.menu_label)
.ThenBy(c => c.menu_value)
.ToList();
Try something like this:
var listSubMenu = (from m in db.TBL_IMS_MENU
join ra in db.TBL_IMS_ROLE_ASSIGNED_MENU on m.MENU_ID
equals ra.MENU_ID
join rp in db.TBL_IMS_USER_ROLE_PRIVILEGES on ra.UROLE_ID
equals rp.GRANTED_ROLE
where rp.USER_ID == UserID
group m by new { m.MENU_ID, m.menu_label, m.menu_value }
into g select g).ToList();
foreach(var groupItem in listSubMenu)
{
// go through groups like this - groupItem.Key.MENU_ID
foreach(var menuItem in grouItem)
{
//go through each item in group like this - menuItem.GRANTED_ROLE
}
}
this.db.transaction(function (tx)
{
tx.executeSql('SELECT tsk.*, cont.firstName, cont.lastName ,cont1.firstName, cont1.lastName, list.listId FROM tbl_tasks tsk, tbl_contacts cont,tbl_contactequests cont1, tbl_lists list WHERE (tsk.assignedId=0 or tsk.assignedId=cont.contactId or tsk.assignedId=cont1.contactRequestId) and tsk.taskCategoryType != "INBOX_NOT_ACCEPTED" and list.listId=tsk.listId and list.listId='+window.defaultlistid+' group by tsk.taskId', [], enyo.bind(this,this.queryResponse), enyo.bind(this,this.errorHandler));//call queryResponse
}.bind(this));
Now as you can see cont.firstName (Table tbl_contacts) & cont1.firstName has (Table tbl_contactequests) has same fields first name
for (var i = 0; i < len; i++)
{
list[i] = results.rows.item(i);
fname = list[i].firstName;
lname = list[i].lastName;
fullname = fname+' '+lname;
//alert(fullname);
if(list[i].assignedId==0)
{list[i].name = '';}
else
{list[i].name=fullname;}
}
with this loop i can able to featch first value of tbl_contacts field firstname.Now suppose if i can't to access tbl_contactequests table firstname.
Use AS keyword for SQL query:
'SELECT tsk.*,
cont.firstName AS cFirstName,
cont.lastName ,
cont1.firstName AS c1FirstName,
cont1.lastName, list.listId
FROM tbl_tasks tsk, tbl_contacts cont,
tbl_contactequests cont1, tbl_lists list
WHERE (tsk.assignedId=0 OR tsk.assignedId=cont.contactId OR
tsk.assignedId=cont1.contactRequestId) AND
tsk.taskCategoryType != "INBOX_NOT_ACCEPTED" AND
list.listId=tsk.listId AND list.listId='+window.defaultlistid+'
GROUP BY tsk.taskId'