when i apply join server show me error
(2/2) QueryException
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
my controller
$player_bet = DB::table('player_bet')
->join('poll', 'player_bet.poll_id = poll.id')
->join('poll_type', 'poll_type.id = poll.poll_type_id')
->join('tournament_week', 'poll.week_id = tournament_week.id')
->join('tournament', 'poll.tournament_id = tournament.id')
->join('users', 'player_bet.users_id = users.id')
->join('poll_winner', 'poll_winner.poll_id = poll.id AND poll_winner.user_id = users.id', 'left')
->where('users.id ='.$user_id)
->select('poll_winner.id AS winner_id, poll_winner.winning_amount AS won_amount,, poll_winner.tax_amount AS won_tax_amount, player_bet.id as bet_id, player_bet.create_time,poll.id poll_id,poll.winning_amount,poll.amount poll_amount,poll.week_id week_id,poll.description,poll.description_detail,poll_type.name poll_type,poll_type.id poll_type_id,tournament_week.week_no week_no, poll.is_close is_close, tournament.id tournament_id, tournament.name tournament_name,tournament.start_date,tournament.end_date,tournament.logo, users.username username')
->where('users_id',$user_id)
->get();
Your join syntax is incorrect: https://laravel.com/docs/7.x/queries#joins
Also Your Where condition is Not proper
Problem 1:
Change the multiple condition join method
->join('poll_winner', 'poll_winner.poll_id = poll.id AND poll_winner.user_id = users.id', 'left')
to
->leftjoin('poll_winner', function($join) {
$join->on('poll_winner.poll_id', '=', 'poll.id')
->whereColumn('poll_winner.user_id', 'users.id');
})
Problem 2:
you have two where condition, the where need to be like this, the one is the column, the other be the column's value:
->where('users.id', $user_id)
Problem 3:
If you want to use one string as parameter of select method, you need to use selecRaw
->selectRaw('poll_winner.id AS winner_id, poll_winner.winning_amount AS won_amount,, poll_winner.tax_amount AS won_tax_amount, player_bet.id as bet_id, ...')
Or you need to use multiple strings as parameters of select method:
->select('poll_winner.id AS winner_id', 'poll_winner.winning_amount AS won_amount', ...)
Related
I'm trying to convert this SQL query into a linq query but not having much luck.
SELECT DISTINCT gen.ID
, gen.Name
, Ssec.System
FROM dbo.Generic Gen
JOIN dbo.SystemsSelected SSel
ON Gen.RecordID = SSel.RecordID
JOIN dbo.Security SSec
ON (
SSel.SystemA = CASE WHEN Ssec.System = '1stSystem' THEN 1 ELSE NULL END
OR SSel.SystemB = CASE WHEN Ssec.System = '2ndSystem' THEN 1 ELSE NULL END
)
and SSec.Username = 'myUserName'
I've had a look at the following posts but cannot apply the examples to my code:
Link join with case condition
Linq query with table joins
Any help would be appreciated!
Thanks
I cannot answer my own question directly but I found a workaround.
I used linq to instead call a Stored Procedure and that worked fine.
To do this:
1) Save code as a Stored Procedure that accepts a parameter named UserName
2) add the SP into Entity Framework Model
3) call the SP using:
public JsonResult GetNewTeams(string userUserName)
{
using (YourDBNameEntities db = new YourDBNameEntities ())
{
var ret = db.SP_YourStoredProcedureName(userUserName).ToList();
return Json(ret, JsonRequestBehavior.AllowGet);
}
}
Thats it, done, no more scary linq!! :)
sauce: Using stored procedures in LINQ
I decomposed a large table into five tables based on BCNF and I tried to join them all together to check if I lost something or not.
I used this sql statement in Sqlite3
Select
spart.Sno, spart.Sname,
cpart.Cno, CtoT.Cname,
CtoT.Tno,tpart.Tname,
scorepart.Degree
from spart, cpart, CtoT, tpart, scorepart
where
cpart.Cname = CtoT.Cname
spart.Sno = scorepart.Sno
CtoT.Tno = tpart.Tno
cpart.Cno = scorepart.Cno;
And I got Error: near "spart": syntax error
Could anybody help me out?
Try this:
Select
spart.Sno, spart.Sname,
cpart.Cno, CtoT.Cname,
CtoT.Tno,tpart.Tname,
scorepart.Degree
from spart, cpart, CtoT, tpart, scorepart
where
(cpart.Cname = CtoT.Cname)
and (spart.Sno = scorepart.Sno)
and (CtoT.Tno = tpart.Tno)
and (cpart.Cno = scorepart.Cno);
I'm not sure the parentheses are really needed. I've forgotten the syntax rules.
I have this raw sql that I am trying to run on grails
final session = sessionFactory.currentSession;
final String query = 'select count(A.*) from Artifact as A LEFT JOIN classification as C on (A.id=C.artifact_id) where C.id IS NULL OR C.active=0';
final sqlQuery = session.createSQLQuery(query);
def totalunclass = sqlQuery.with {
addEntity(Artifact);
}
But I am getting the error Message: SQL queries do not currently support returning aliases
I tried another method but failed there too achieve the result click here
Try changing your query line to look like this
final String query = 'select count(*) from Artifact A LEFT JOIN classification C on (A.id=C.artifact_id) where C.id IS NULL OR C.active=0';
Try this and remove aliases:
final session = sessionFactory.currentSession;
final String query = 'select count(Artifact.*) from Artifact LEFT JOIN classification on (Artifact.id=classification.artifact_id) where classification.id IS NULL OR classification.active=0';
final sqlQuery = session.createSQLQuery(query);
def totalunclass = sqlQuery.with {
addEntity(Artifact);
}
Wanting to pass a single codeigniter query result to my controller using json.
my model function is:
function get_linked_loads_qty($q){
$sql = $this->db->query("
IF EXISTS(
select qtylinkedorders
from Linked_Order_Summary
join Linked_Order_lines
on Linked_Order_Summary.linkedorderid= Linked_Order_lines.linked_order_id
where load_number='$q'
)
begin
select qtylinkedorders
from Linked_Order_Summary
join Linked_Order_lines
on Linked_Order_Summary.linkedorderid= Linked_Order_lines.linked_order_id
where load_number='$q'
END
ELSE BEGIN
select 1 as qtylinkedorders
from customer_Order_lines
where CustomerOrderID='$q'
group by CustomerOrderID
END
");
$query = $this->db->get();
if($query->num_rows > 0){
foreach ($query->result_array() as $row){
$row_set[] = htmlentities(stripslashes($row[qtylinkedorders])); //build an array
}
return $row_set;
}
}
and my controller is:
function get_linked_loads_qty(){
$this->load->model('Sales_model');
if (isset($_POST['data'])){
$q = strtolower($_POST['data']);
$data = $this->Sales_model->get_linked_loads_qty($q);
$this->output->set_content_type('application/json')->set_output(json_encode($data));
}
}
The sql works 100% in mssql. the codeigniter error is:
<p>Error Number: 42000</p><p>[Microsoft][SQL Server Native Client 11.0][SQL Server]Must specify table to select from.</p><p>SELECT *</p>
as mentioned the query executes perfectly in mssql.
any assistance welcome. Thanks.
I believe the $this->db->get() is not needed, and is what causes the error. The $this->db->query("blah...") will run the query and return the result. See CodeIgniter DB Query Docs
I have the following code.
var $uses = array('TABLE1','TABLE2','TABLE3');
function test() {
$this->Table1->updateAll(
array('Table1.field1' => 'some value'),
array('Table1.field1 ' => 'some condition')
);
......
.....
problem is that when I try to update only one table...Table1, it joins other tables with it.
UPDATE
`Table1`
LEFT JOIN
`Table2`
ON
(`Table1`.`id` = `Table2`.`uid`)
LEFT JOIN
`Table3`
ON
(`Table1`.`Table3_id` = `Table3`.`id`)
SET
`Table1`.`field1` = 1
WHERE
some condition.......
How can I not join the table and run update only on Table1?
Edit:
I used this but did not work :
$this->Table1->unBindModel(array(hasMany => array('Table2', 'Table3')));
firstly, stop using $uses. it will only cause you more pain than needed.
to avoid the joins use Model::unbindModel(array('relationType' => array('Relation')) http://book.cakephp.org/view/1045/Creating-and-Destroying-Associations-on-the-Fly
Try setting your recursive level to -1. I'm not sure whether this affects Update calls but it's worth a try.
$this->Table1->recursive = -1;
$this->Table1->updateAll(...);