Hi there (I'm new to PHP),
I can't quite figure out the syntax of a subquery I'm trying to make, this is the query:
SELECT * FROM show_episode, shows, show_episode_airdate, show_moyenne
WHERE season = 1 AND episode = 1
AND shows.imdb_id = show_episode.imdb_id_show
AND show_episode_airdate.episode_id = show_episode.episode_id
AND show_moyenne.show_id = shows.id
AND show_episode_airdate.airdate < '2013-07-12'
ORDER BY show_episode_airdate.airdate DESC LIMIT 10
Once this was done, I wanted to order those 10 selected rows by show_moyenne.moyenne with something like that:
SELECT * (FROM show_episode, shows, show_episode_airdate, show_moyenne
WHERE season = 1 AND episode = 1
AND shows.imdb_id = show_episode.imdb_id_show
AND show_episode_airdate.episode_id = show_episode.episode_id
AND show_moyenne.show_id = shows.id
AND show_episode_airdate.airdate < '2013-07-12'
ORDER BY show_episode_airdate.airdate DESC LIMIT 10)
* ORDER BY show_moyenne.moyenne DESC
Which is not correct, anybody can show me the right way to do this ?
Thanks, any help appreciated!
Select * from
(SELECT * FROM show_episode, shows, show_episode_airdate, show_moyenne
WHERE season = 1 AND episode = 1
AND shows.imdb_id = show_episode.imdb_id_show
AND show_episode_airdate.episode_id = show_episode.episode_id
AND show_moyenne.show_id = shows.id
AND show_episode_airdate.airdate < '2013-07-12'
ORDER BY show_episode_airdate.airdate DESC LIMIT 10) as j
order by j.moyenne DESC
I hope this can be of some help.
SELECT x.*
FROM
( SELECT *
FROM show_episode e
JOIN shows s
ON s.imdb_id = e.imdb_id_show
JOIN show_episode_airdate a
ON a.episode_id = e.episode_id
JOIN show_moyenne m
ON m.show_id = s.id
WHERE season = 1
AND episode = 1
AND a.airdate < '2013-07-12'
ORDER
BY a.airdate DESC
LIMIT 10
) x
ORDER
BY moyenne;
Related
I'm just starting with MySQL so I would like to know how can I select only 5 random rows in the last 50 entries of my database? I hope you understand my question.
I'm using PDO and what I have now is this:
$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);
Then I use a PHP foreach loop...
Thank you
The challenge is determining the last 50 entries. Assuming you have an auto-incremented id, you can do:
SELECT a.*
FROM (SELECT a.*
FROM articulos a
WHERE cat = '$ArtCat'
ORDER BY id DESC
LIMIT 50
) a
ORDER BY RAND()
LIMIT 5;
The key idea is the subquery to get the last 50 entries, and then the final query to get the 5 random rows. The subquery needs to specify how you identify the last 50.
$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() LIMIT 5 ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);
just add limit
i assume in your table you have some date column so we can get last 50
SELECT * FROM (SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY created_tiem desc limit 50 ) t order by RAND() limit 5;
Is it possible to get these two statements into one query?
$q = mysqli_query($con,"SELECT * FROM angebote where (city = '".$_POST['search']."' OR plz = '".$_POST['search']."') AND art = 'biete' ORDER BY id DESC LIMIT 18");
$p = mysqli_query($con,"SELECT * FROM angebote where (city = '".$_POST['search']."' OR plz = '".$_POST['search']."') AND art = 'suche' ORDER BY id DESC LIMIT 18");
doesn't seem to work with UNION or UNION ALL
SO when I tried
$q = mysqli_query($con,"SELECT * FROM angebote where (city = '".$_POST['search']."' OR plz = '".$_POST['search']."') AND art = 'biete' ORDER BY id DESC LIMIT 10 UNION
SELECT * FROM angebote where (city = '".$_POST['search']."' OR plz = '".$_POST['search']."') AND art = 'biete' ORDER BY id DESC LIMIT 10");
it says Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:.....
UNION works fine here:
$q = mysqli_query($con, "
SELECT * FROM
(SELECT
*
FROM
angebote
WHERE
(city = '".$_POST['search']."' OR plz = '".$_POST['search']."') AND
(art = 'biete')
ORDER BY id DESC LIMIT 10) as biete_tabelle
UNION
SELECT * FROM
(SELECT
*
FROM
angebote
WHERE
(city = '".$_POST['search']."' OR plz = '".$_POST['search']."') AND
(art = 'suche')
ORDER BY id DESC LIMIT 10) as suche_tabelle"
);
This will get you the results you look for. Have a look here for an example.
Note: Though I sticked with german for consistency, I would like to advice you, that it is best bractice to use column and variable etc. names in english, so it is easier for developers that do not speak german to read your code.
I have been having problems with this query for close to 3 hours and no amount of googling is helping me so far:
select id,nombres,apaterno,amaterno, (select sum(cargo) from tb_consultorios_recibos_transacciones where id_paciente = tb_consultorios_pacientes.id and date(fecha_trans)>='2015-03-01' and date(fecha_trans)<='2015-03-31') as cargospaciente, (select sum(abono) from tb_consultorios_recibos_transacciones where id_paciente = tb_consultorios_pacientes.id and date(fecha_trans)>='2015-03-01' and date(fecha_trans)<='2015-03-31') as abonospaciente from tb_consultorios_pacientes where id_consultorio = 3 order by apaterno asc, cargospaciente desc
besides the where clause, I would like only to display rows where the alias cargospaciente or abonospaciente are greater than 0, this is the query I am trying which is obviously not working:
select id,nombres,apaterno,amaterno, (select sum(cargo) from tb_consultorios_recibos_transacciones where id_paciente = tb_consultorios_pacientes.id and date(fecha_trans)>='2015-03-01' and date(fecha_trans)<='2015-03-31') as cargospaciente, (select sum(abono) from tb_consultorios_recibos_transacciones where id_paciente = tb_consultorios_pacientes.id and date(fecha_trans)>='2015-03-01' and date(fecha_trans)<='2015-03-31') as abonospaciente from tb_consultorios_pacientes where id_consultorio = 3 having (cargospaciente>0 or abonospaciente>0) order by apaterno asc, cargospaciente desc
any help on how to specify having and where in the same clause?
Try it like below rather; by getting the sum of required column with proper grouping and then join that result set with outer select result
select tcp.id,
tcp.nombres,
tcp.apaterno,
tcp.amaterno,
tab.sum_cargo,
tab.sum_abono
from tb_consultorios_pacientes tcp
join
(
select id_paciente, sum(cargo) as sum_cargo, sum(abono) as sum_abono
from tb_consultorios_recibos_transacciones
where date(fecha_trans)>='2015-03-01'
and date(fecha_trans)<='2015-03-31'
group by id_paciente
having sum_cargo > 0 or sum_abono > 0
) tab
on tcp.id = tab.id_paciente
where tcp.id_consultorio = 3
order by tcp.apaterno asc, tcp.cargospaciente desc
I tried something like this:
$changed = mysql_query("SELECT * FROM games WHERE changed = 'y' AND human = '$_GET[human]' ORDER BY id DESC LIMIT 100", $link);
$num_rowsc = mysql_num_rows($changed);
So what i want is to select last 100 where human = yes and count where changed = y ..
so get last 100 humans and count how many of them have on changed yes
Are you looking for something like this :
$stmt = $pdo->prepare("SELECT COUNT(*) AS cnt FROM ( SELECT * FROM games
WHERE human= :human ORDER BY id DESC LIMIT 100
) WHERE changed = 'y' ");
$stmt->bindParam(':human', $_GET[human]);
$stmt->execute();
Maybe with this query?
SELECT COUNT(*) AS cnt FROM (
SELECT * FROM games
WHERE human = '".mysql_real_escape_string($_GET[human])."'
ORDER BY id DESC LIMIT 100
) tmp WHERE changed = 'y'
1) try using this as your query:
SELECT count(*) FROM (SELECT * FROM games WHERE changed = 'y' ORDER BY id DESC LIMIT 100) WHERE human = '$_GET[human]'
2) use mysqli
select count(*) as cnt from (
SELECT human FROM games WHERE human = '$_GET[human]' ORDER BY id DESC LIMIT 100
) as changes WHERE changed = 'y'
What's the best, and easiest way to do this? My query currently is:
SELECT *
FROM chat
WHERE (userID = $session AND toID = $friendID)
OR (userID = $friendID AND toID = $session)
ORDER BY id
LIMIT 10
This shows the first 10 rows though, not the last 10.
EDIT: I Want the last 10 rows (Which yes, DESC does this) However I want them to be returned in ASCENDING order.
to reverse the order (therefore get last 10 instead of first 10), use DESC instead of ASC
EDIT
Based on your comment:
SELECT * FROM (
SELECT *
FROM chat
WHERE (userID = $session AND toID = $friendID)
OR (userID = $friendID AND toID = $session)
ORDER BY id DESC
LIMIT 10
) AS `table` ORDER by id ASC
If you want the last 10 then just change ASC to DESC
SELECT *
FROM
chat
WHERE
(userID=$session AND toID=$friendID)
OR
(userID=$friendID AND toID=$session)
ORDER BY id
DESC
LIMIT 10
$con = mysqli_connect("localhost","my_user","my_password","my_db");
$limit = 10;
$query = "SELECT * FROM $table";
$resource = mysqli_query($con,$query);
$total_rows = mysqli_num_rows($resource);
$start = $total_rows-$limit;
$query_limit= $query." LIMIT $start,$limit";
First I have set the limit
$limit = 10;
then
$total_rows = mysqli_num_rows($resource);
Here I have taken total number of rows affected.
$start = $total_rows-$limit;
then substracted limit from number of rows to take starting record number
$query_limit= $query." LIMIT $start,$limit";
and then added limit to the query.
For more information about limit see this link
https://www.w3schools.com/php/php_mysql_select_limit.asp
First select the last 10 from the table, then re-order them in ascending order.
SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 10) sub ORDER BY id ASC