I know this query is not correct, but how should I write it?
UPDATE OFFER SET
IF(St != 3, St = 2 AND View = '$userupdate' AND Date_V_E = '$date', '')
WHERE Id = '$entryID'
The IF should be like this:
if(St != 3){
St = 2;
View = '$userupdate';
Date_V_E = '$date';
}else{
//DO NOTHING
}
UPDATE OFFER
SET St = 2,
View = '$userupdate',
Date_V_E = '$date'
WHERE Id = '$entryID'
and St <> 3
Related
I am getting the following error
"Lookup Error - MySQL Database Error: You can't specify target table
'customer_copy' for update in FROM clause"
while executing the following query:
Delete from customer_copy where code IN
(select cuc.code from customer_copy cuc
INNER JOIN customer_copy1 cu ON cuc.code = cu.code
where cuc.district=cu.district and cuc.city = cu.city and
cuc.name = cu.name and cuc.country = cu.country and
cuc.customer_grp1 = cu.customer_grp1 and
cuc.email = cu.email and cuc.fax = cu.fax and
cuc.house_number = cu.house_number and
cuc.name=cu.name and cuc.postal_code = cu.postal_code and
cuc.region = cu.region and cuc.street = cu.street and
cuc.tax_juris_code = cu.tax_juris_code and
cuc.tax_number1 = cu.tax_number1 and
cuc.tele = cu.tele and cuc.plant_code = cu.plant_code and
cuc.employee_code = cu.employee_code and
cuc.sales_district_code = cu.sales_district_code and
cuc.sales_office_code = cu.sales_office_code and
cuc.tax_number2 = cu.tax_number2 and
cuc.tax_number3 = cu.tax_number3 and
cuc.search_terms_1 = cu.search_terms_1 and
cuc.search_terms_2 = cu.search_terms_2);
in MYSQl database
The select query is working fine. What can possibly be wrong?
Instead of Delete from tableName just write Delete tableName
I have solve this problem. I had to use an alias name for the inner query. Don't know why but I have made the query run!!
here is my updated query.
delete cc.* from customer_copy cc where cc.code IN
(select code from (select cu.code from customer_copy cu
INNER JOIN customer_copy1 cuc ON cuc.code = cu.code
where cuc.district=cu.district and cuc.city = cu.city and
cuc.name = cu.name and cuc.country = cu.country and
cuc.customer_grp1 = cu.customer_grp1 and
cuc.email = cu.email and cuc.fax = cu.fax and
cuc.house_number = cu.house_number and
cuc.name=cu.name and cuc.postal_code = cu.postal_code and
cuc.region = cu.region and cuc.street = cu.street and
cuc.tax_juris_code = cu.tax_juris_code and
cuc.tax_number1 = cu.tax_number1 and
cuc.tele = cu.tele and cuc.plant_code = cu.plant_code and
cuc.employee_code = cu.employee_code and
cuc.sales_district_code = cu.sales_district_code and
cuc.sales_office_code = cu.sales_office_code and
cuc.tax_number2 = cu.tax_number2 and
cuc.tax_number3 = cu.tax_number3 and
cuc.search_terms_1 = cu.search_terms_1 and
cuc.search_terms_2 = cu.search_terms_2)x);
TSQL:-
Update table1
Set Name = 'John',
Address = null
where
ID = 1
LINQ-TO-SQL
var tab = db.Table1.Single(s => s.ID == 3);
tab.Name = DateTime.Now;
tab.Address = null;
db.SubmitChanges();
There isn't a single LINQ to SQL statement for updates. You have to retrieve the object, modify it, then save the changes (code assumes a single row since you have a specific Id):
var entity = context.Table1.Single(t => t.Id == 1);
entity.Name = "John";
entity.Address = "Toronto";
context.SubmitChanges();
using (var dataContext = new MyEntities())
{
var contact = Contacts.Single (c => c.ContactID == 1);
contact.FirstName = 'John';
contact.Address= 'Toronto';
dataContext.SaveChanges();
}
I have a Table structure as
id, trackid, table_name, operation,
oldvalue, newvalue, field,
changedonetime
Now if I have 3 rows for the same "trackid" same "field", then how can i select the latest out of the three?
i.e. for e.g.:
id = 100 trackid = 152 table_name
= jos_menu operation= UPDATE oldvalue = IPL newvalue = IPLcccc
field = name live = 0 changedonetime =
2010-04-30 17:54:39
and
id = 101 trackid = 152 table_name =
jos_menu operation= UPDATE oldvalue
= IPLcccc newvalue = IPL2222 field = name live = 0 changedonetime =
2010-04-30 18:54:39
As u can see above the secind entry is the latest change,
Now what query I should use to get the only one and Latest row out of many such rows...
$distupdqry = "select DISTINCT trackid,table_name from jos_audittrail where live = 0 AND operation = 'UPDATE'";
$disupdsel = mysql_query($distupdqry);
$t_ids = array();
$t_table = array();
while($row3 = mysql_fetch_array($disupdsel))
{
$t_ids[] = $row3['trackid'];
$t_table[] = $row3['table_name'];
//$t_table[] = $row3['table_name'];
}
//echo "<pre>";print_r($t_table);echo "<pre>";
//exit;
for($n=0;$n<count($t_ids);$n++)
{
$qupd = "SELECT * FROM jos_audittrail WHERE operation = 'UPDATE' AND trackid=$t_ids[$n] order by changedone DESC ";
$seletupdaudit = mysql_query($qupd);
$row4 = array();
$audit3 = array();
while($row4 = mysql_fetch_array($seletupdaudit))
{
$audit3[] = $row4;
}
$updatefield = '';
for($j=0;$j<count($audit3);$j++)
{
if($j == 0)
{
if($audit3[$j]['operation'] == "UPDATE")
{
//$insqry .= $audit2[$i]['operation']." ";
//echo "<br>";
$updatefield .= "UPDATE `".$audit3[$j]['table_name']."` SET ";
}
}
if($audit3[$j]['operation'] == "UPDATE")
{
$updatefield .= $audit3[$j]['field']." = '".$audit3[$j]['newvalue']."', ";
}
}
/*echo "<pre>";
print_r($audit3);
exit;*/
$primarykey = "SHOW INDEXES FROM `".$t_table[$n]."` WHERE Key_name = 'PRIMARY'";
$prime = mysql_query($primarykey);
$pkey = mysql_fetch_array($prime);
$updatefield .= "]";
echo $updatefield = str_replace(", ]"," WHERE ".$pkey['Column_name']." = '".$t_ids[$n]."'",$updatefield);
}
In the above code I am fetching ou the distinct IDs in which update operation has been done, and then accordingly query is fired to get all the changes done on different fields of the selected distinct ids...
Here I am creating the Update query by fetching the records from the initially described table which is here mentioned as audittrail table...
Therefore I need the last made change in the field so that only latest change can be selected in the select queries i have used...
please go through the code.. and see how can i make the required change i need finally..
This is another question of the greatest-n-per-group category, which comes up several times per week on Stack Overflow.
Here's how I'd solve it in your case:
SELECT j1.*
FROM jos_audittrail j1 LEFT OUTER JOIN jos_audittrail j2
ON (j1.trackid = j2.trackid AND j1.field = j2.field
AND j1.changedonetime < j2.changedonetime)
WHERE j2.id IS NULL;
I am fetching data from database to textbox using Linq.When i try update the same textbox value,it does not work.
DAL.TournamentsDataContext tdc = new SchoolSports.DAL.TournamentsDataContext();
var tournamentTable = tdc.GetTable<DAL.Tournament>();
var tournamentRecord = (from rec in tournamentTable
where rec.TournamentId == TournamentId
select rec).Single();
tournamentRecord.Tournament_type = Tournament_type;
tournamentRecord.Tournament_Name = Tournament_Name; ;
tournamentRecord.Tournament_Level = Tournament_Level;
tournamentRecord.Tournament_For = Tournament_For;
tournamentRecord.Country_Code = Country_Code;
tournamentRecord.Tournament_Status = Tournament_Status;
tournamentRecord.Tournament_begin_date = Tournament_begin_date;
tournamentRecord.Tournament_end_date = Tournament_end_date;
tournamentRecord.Sponsored_By = Sponsored_By;
tournamentRecord.Tournament_Details = Tournament_Details;
var organiserTable = tdc.GetTable<DAL.Organiser>();
var organiserRecord = (from rec in organiserTable
where rec.Tournament_Id == TournamentId
select rec).Single();
organiserRecord.Name_Of_Organiser = OrName;
organiserRecord.Telephone = OrTeleNo;
organiserRecord.Email = OrEmail;
organiserRecord.Mobile = OrMobile;
organiserRecord.Fax = OrFax;
if (Tournament_For == "School")
{
var invitedSchoolIdTable = tdc.GetTable<DAL.Invited_School>();
var invitedSchoolIdRecord = (from rec in invitedSchoolIdTable
where rec.Tournament_Id == TournamentId
select rec).Single();
invitedSchoolIdRecord.School_Ids = SchoolUniIds;
}
if (Tournament_For == "University")
{
var invitedUniversityTable = tdc.GetTable<DAL.Invited_University>();
var invitedUniversityIdRecord = (from rec in invitedUniversityTable
where rec.Tournament_Id == TournamentId
select rec).Single();
invitedUniversityIdRecord.University_Ids = SchoolUniIds;
}
tdc.SubmitChanges();
(The following answer was written before he updated the question)
If you want the value to be updated with the value in the text box, the text box must be bound to a datasource
I have solved using IsPostBack in Page_Load Event.
I have SQL database as follows
alt text http://img97.imageshack.us/img97/5774/dbimage.jpg
Now I want to filter the restaurant_detail table for the parameters:
1. cuisine 2. area
Can you help me to build LINQ query?
I presume you have a model generated either with LINQ to SQL or Entity Framework. Also, I'm assuming foreign key relationships have been set.
var details = db
.Cuisines
.Where(c => c.Cuisine=="something")
.SelectMany(c => c.RestaurantCuisines)
.Select(rc => rc.Restaurant.RestaurantDetails)
.Where(rd => rd.Area=="something")
;
Done with the linq query using following lines of code :
c = from q in dc.restaurant_cuisines
where q.cuisine.cuisine1.Contains(cuisine)
&& q.restaurant.price.ToString().Length == price.Length
select new NearBy { NearById = q.restaurant.id, NearByLongitude = (double)q.restaurant.longitude, NearByLatitude = (double)q.restaurant.latitude };
}
int[] ids = new int[c.Count()];
var lon = from q1 in dc.area_maps where q1.area.ToLower() == area.ToLower() select q1.longtitude;
var lat = from q1 in dc.area_maps where q1.area.ToLower() == area.ToLower() select q1.latitude;
foreach(NearBy n in c)
{
result = calcDistNew((double)lat.FirstOrDefault(), (double)lon.FirstOrDefault(), n.NearByLatitude, n.NearByLongitude);
ids[i++] = n.NearById;
}
var r = from q in dc.restaurant_details
where 1 == 1 &&
(ids).Contains(q.restaurant_id)
select new Restaurant
{
Restora_id = q.restaurant_id.ToString(),
Name = q.restaurant.name,
Foodtype = q.restaurant.foodtype.foodtype1,
Avg_rating = q.restaurant.avg_rating.ToString(),
Featured = q.restaurant.featured.ToString(),
CuisineList = getCuisine(q.restaurant_id),
Restora_type = q.type,
Distance = Math.Round(calcDistNew((double)lat.FirstOrDefault(), (double)lon.FirstOrDefault(), (double)q.restaurant.latitude, (double)q.restaurant.longitude), 2),
Newarrival = q.restaurant.newarrival.ToString(),
CountRecord = ids.Length.ToString()
};
var d = r.AsEnumerable().OrderBy(t => t.Distance);
var g = d.Take(recordSize + 10).Skip(recordSize);
return g.ToList();
Please note that above displayed code generated with some changes from the initial requirements.