How to make Select from all table ids except the first 2?

Asked

Viewed 274 times

-1

I need to make a select in mysql of all users of a table, but I cannot receive the first two users of the table and I need to do this without specifying id, since when more users are registered I need the first two not to appear. I tried some things like NOT EXISTS (Other Select), but it didn’t help. I can’t update my SQL so I can’t use LIMIT, IN, ALL, ANY AND SOME. Does anyone know? If there’s another way you can tell me too.

1 answer

1

If you are using PHP you can create a query that returns the -2(first two lines) amount of record and use the value obtained to call the results, except the first two lines

Edited Response

<?php
//carrega arquivo de conexão do banco
include 'dbConfig.php';
?>


<?
// conta registros
$queryTotal = $db->query("SELECT count(id)-2 as id FROM tabela");
?>
<?php foreach($queryTotal as $row) : ?> 
<?php endforeach ?>

<!-- cria variavel de quantidade de registros -2 -->
<?$total = $row['id'];?>

<!-- chama registros menos os 2 primeiros usando a variável gerada acima -->
<?
$query = $db->query("SELECT id FROM tabela order by id DESC LIMIT $total");
    while($row = $query->fetch_assoc()){ 

} ?>

            <!--  imprime valores na tela-->
            <?php foreach($query as $row) : ?> 
            <?php echo $row['id']; ?></p>
            <?php endforeach ?> 
  • Miguel, I don’t know if you read the other comment but, I tried to do what you said and even if sql gave me the message that can not use LIMIT. When I implemented the select you sent me, the page returned this error: ERRORSQLSTATE[42000]: Syntax error or access Violation: 1064 You have an error in your SQL syntax; check the manual that Corresponds to your Mariadb server version for the right syntax to use near '(SELECT Count(id) FROM bd_post ORDER BY id)' at line 1, you know what the problem would be?

  • Codes I’m using: $select = "SELECT Count(id) FROM bd_post ORDER BY id"; $Select2 = "SELECT * FROM bd_post ORDER BY id DESC LIMIT ($select)";

  • @Marcuslopes if you can put the full query in the question helps a lot, try to remove the parenthesis of LIMIT ($select)

  • Even removing the parentheses he gives the same error: ERRORSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT count(id) FROM bd_postagem ORDER BY id' at line 1 , basically what I need is for it to select all the fields of that table but ignoring all the fields of the last two ids.

  • I’m using these two codes $select2 = "SELECT count(id)-2 FROM bd_postagem ORDER BY id";&#xA; $select3 = "SELECT * FROM bd_postagem ORDER BY id DESC LIMIT $select2";

  • I’m doing the query via Pdo: try&#xA; {&#xA;&#xA; $result2 = $conexao -> prepare($select3);&#xA; $result2 ->execute();&#xA; $contar2 = $result2 ->rowCount();&#xA;&#xA; if ($contar2 > 0) &#xA; {&#xA; while ($show = $result2->FETCH(PDO::FETCH_OBJ))&#xA; { inside that while is the code to display what select bring me

  • @Marcuslopes edited the answer, see if it helps you in any way, adapt to your query, tested here and worked well.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.