PDO connection select last id from table [duplicate] - pdo

This question already has answers here:
Get the last insert id with doctrine 2?
(8 answers)
Closed 6 years ago.
I am trying to get the last id from my selected table. I know lastinsertid(); sould work but it keeps returning a 0. What am i doing wrong here?
public function getlastadded()
{
$Sql = 'SELECT * FROM agendaevent';
$Stm = $this->getEntityManager()->getConnection()->prepare($Sql);
$Stm->execute();
return $this->getEntityManager()->getConnection()->lastInsertId('id');
}
ps i am working in symfony if that makes a difference.

Try this, friend:
"SELECT * FROM agendaevent ORDER BY id DESC LIMIT 1"

Related

How can I update this column with an aggregate function? [duplicate]

This question already has answers here:
SQL update query using joins
(13 answers)
Closed 2 years ago.
I calculated a sum for OREB that is unique to every pair of GameId and TeamId
using this query,
Query Image.
select DGAM.GameID, HomeTeamID, SUM(OREB) as OREB_Home
from dimGames as DGAM
join dimPerformance as DPERF on DGAM.GameID = DPERF.GAME_ID and DGAM.HomeTeamID = DPERF.TEAM_ID
group by DGAM.GameID, HomeTeamID
Using the results of this query I want to update the null column in this table
Table called dimGames.
How can I write an update statement to accomplish this?
Adapting this answer, I think you want:
with agg as (
select DGAM.GameID, HomeTeamID, SUM(OREB) as OREB_Home
from dimGames as DGAM
join dimPerformance as DPERF on DGAM.GameID = DPERF.GAME_ID and DGAM.HomeTeamID = DPERF.TEAM_ID
group by DGAM.GameID, HomeTeamID
)
UPDATE dimGames dg
SET dg.OREB_home = agg.OREB_Home
FROM agg
WHERE dg.GameId = agg.GameId and dg.HomeTeamId = agg.HomeTeamId

Select max(date) in Firebird [duplicate]

This question already has answers here:
Select first row in each GROUP BY group?
(20 answers)
Firebird Query- Return first row each group
(4 answers)
Closed 5 years ago.
I have a table in a Firebird 2.5 Database where I have to find out the latest entry for one specific articel no. Therefore the table has a colum "date".
I tried several queries but I always get more than 1 entry for the article no.
I used the following query:
select max(lp.datum) as Wareneingangsdatum, lp.nr, lp.artikelnr, lp.belegnr, lf.nr, lf.name, lf.plz, lf.ort, lf.laenderkuerzelnr, lae.bezeichnung
from lagerprotokoll lp
left join waeinpos we on we.idnr = lp.belegposidnr
left join waein we1 on we1.nr = we.nr
left join liefrant lf on lf.nr = we1.lieferantnr
left join laender lae on lae.nr = lf.laenderkuerzelnr
where lp.belegtyp = 14
group by 2,3,4,5,6,7,8,9, 10
order by 1 desc
How can I achieve the result so that I can only get the latest entry for one specific article in the table.

PostgreSQL check value from sum in where [duplicate]

This question already has answers here:
Referring to a Column Alias in a WHERE Clause
(9 answers)
Closed 7 years ago.
I'm triyng to add a the done value to where clause, so I can show only results from todo, that are already done, but keep getting an error:
select "todo", SUM( "test"."count" ) as "done"
from B
Where "done" = "todo"
this is the error:
q_driver: [PGRES_FATAL_ERROR]ERRORE: the column "done" don't exist
but the column is displayed if I remove the where clause
You cannot use alias name in same select query where clause
select * from
(
select "todo", SUM( "test"."count" ) as "done"
from B
GROUP BY "todo"
)
Where "done" = "todo"

SQL Return the date on which the sold 10 products passes per parameter [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hello i need to know when (date) the 10 (tenth) item as been sold, the ID item is passed per parameter,
this is the sales tables:
Thks in advance
Please try the following (I believe* that I used the correct columns):
// This example finds the date that the 10th occurrence of Part #1001 was sold.
// #idArtigo is the placeholder for your incoming parameter
DECLARE #idArtigo int;
SET #idArtigo = 1001;
WITH Artigos AS (
SELECT
Row_Number() OVER(ORDER BY v.[DataHora] ASC, lv.[IdLinhaVenda] ASC) AS RowNumber,
v.[DataHora],
v.[IdVenda],
lv.[IdLinhaVenda]
FROM
[Vendas] AS v
INNER JOIN [LinhasVenda] AS lv
ON ( v.[IdVenda] = lv.[IdVenda] )
WHERE
lv.[IdArtigo] = #idArtigo
)
SELECT
[DataHora],
[IdVenda],
[IdLinhaVenda]
FROM
Artigos
WHERE
RowNumber = 10;
*NOTE: I do not know Portuguese (other than via Google Translate), so I made some educated guesses (& assumptions) as to which columns to use.

confusing query [duplicate]

This question already has answers here:
Grouping WHERE clauses in Codeigniter
(6 answers)
Closed 9 years ago.
I am really sorry to ask this silly question but I am a bit confused about the query.
$this->db->select('user_package.status,user_package.remaining,user_package.date,package.pname,package.pcount');
$this->db->join('user_package','user_package.pid=package.pid','left');
$this->db->where('user_package.status','Active');
$this->db->or_where('user_package.status','Pending');
$this->db->where('user_package.uid',$rows->uid);
$user_package=$this->db->get('package');
and here is the similar query in mysql panel which i run for result to check the out put
Select up.status,up.remaining,up.date,p.pname,up.uid,p.pcount FROM user_package up LEFT JOIN package p ON up.pid=p.pid where up.status="Active" OR up.status="Pending" AND up.uid=7
and here is the output of the query
What i want is that find only those rows whose uid is 7 and check if their status is Pending or Active. but the output is different. it takes out Pending and Active records with 7 and 8 uid.
Instead of using $this->db->where try using $this->db->where_in
This should limit the results to only the IDs you require.
e.g.
$this->db->select('user_package.status,user_package.remaining,user_package.date,package.pname,package.pcount');
$this->db->join('user_package','user_package.pid=package.pid','left');
$this->db->where('user_package.status','Active');
$this->db->or_where('user_package.status','Pending');
$this->db->where_in('user_package.uid',$rows->uid);
$user_package=$this->db->get('package');
You have to do it like this
$select = array(
'up.status',
'up.remaining',
'up.date',
'p.pname',
'up.uid',
'p.pcount'
);
$status = array('Active','Pending');
$this->db
->select($select)
->from('user_package up')
->join('package p','up.pid = p.pid','left')
->where_in('up.status',$status)
->where('up.uid',$rows->uid)
->get();