Kindly let me know how can I transform the following query in a way that it'll work perfectly in ms access:
$sql = "SELECT * FROM Registration Limit 100,200";
I tried to use the following but it didnot work the way above query works in SQL.
$sql = "SELECT TOP 100,200 * FROM Registration";
You can't do it directly; Access doesn't support either of the LIMIT or TOP <countstart>, <countend> statements.
You can work around it, if you have an auto-increment (identity) column in your table (or something you can use instead to order rows):
SELECT
Top 100 reg.*
FROM
registration reg
WHERE
reg.RegistrationID >
(
SELECT
Top 100 r.RegistrationID
FROM
registration r
ORDER BY
r.RegistrationID
)
ORDER BY
reg.RegistrationID
Related
I'm trying to get my site to only output a specific amount of data from my database, and not just the whole thing. Can anyone help me with this? Im trying to get it to display the first 25 lines of data.
Right now my php code, is as follows:
$sql = "SELECT * FROM cards WHERE name LIKE '%$query%';";
$sql = "SELECT * FROM cards WHERE name LIKE '%$query%' LIMIT 10;";
if you are using MySql u can use LIMIT to get a limited number of records.
If you are using MSSQL you can use TOP
hope this helps.
Try it:
$sql = "SELECT * FROM cards WHERE name LIKE '%$query%' LIMIT 0,25;"
I'm making 4 individual queries to a MySQL DB, all of which are identical except the WHERE parameters. 2 of which are:
$totalInvites = mysqli_num_rows(mysqli_query($con, "SELECT code FROM invites"));
$usedInvites = mysqli_num_rows(mysqli_query($con, "SELECT code FROM invites WHERE used IS NOT NULL"));
Is there a way of doing the $totalInvites query and from the returned table, do the WHERE call without doing another query?
If that's confusing, this is an example of what I mean:
$query = mysqli_query($con, "SELECT code FROM invites");
$totalInvites = mysqli_num_rows($query);
$usedInvites = mysqli_num_rows($query /*WHERE used IS NOT NULL*/);
I know that's not proper syntax but that's what I was trying say.
If you just want counts then retrieving the entire database and throwing out the results is not really a good idea. Instead jus task for a count:
SELECT COUNT(*) AS count, used
GROUP BY used
This will give you up to two rows, one count for those that are used and one that isn't presuming used has only NULL or a single non-null value.
Use as little SQL as possible:
SELECT if(used is null, 0, 1) AS used, code
FROM invites
And parse result in PHP according to what you need
SELECT
(SELECT code FROM invites) total
(SELECT code FROM invites WHERE used IS NOT NULL) used
im making a simple admin module to query the database to show the results. Im using this query via php:
SELECT
*
FROM myTable
WHERE id in(SELECT
id_registro
FROM myOtherTable
where id_forma='".$id_club."' and fecha_visita Like '%".$hoy."%'
)
order by id DESC
The result shows, however, it takes very long like 2 minutes..Anyone can help me out?
Thanks!
Without seeing your database, it is hard to find a way to make it faster.
Maybe you can try to turn your WHERE IN to INNER JOIN. To something like this
SELECT * FROM myTable INNER JOIN myOtherTable
ON (myTable.id = myOtherTable.id_registro)
WHERE myOtherTable.id_forma = '$id_club'
AND myOtherTable.fecha_visita LIKE '%$hoy%'
ORDER BY myTable.id DESC
Noted that you should sanitize your variable before putting it SQL query or using PDO prepare statement.
Sub Queries takes always time, so its better to ignore them as much as possible.
Try to optimize your query by checking its cardinality,possible keys getting implemented by DESC or EXPLAIN , and if necessary use FORCE INDEX over possible keys.
and I guess you can modify your query as:
SELECT
*
FROM myTable
inner join id_registro
on (id = id_forma )
where
id_forma='".$id_club."' and fecha_visita Like '%".$hoy."%'
order by id DESC
LIKE in mysql may take a long time ,with or without index.
Do u have a very large DB?
I have been working on try to create php web-based paging for our tables
which have over a million rows.
Based on what I have read, I have 3 options
retrieve all rows in resultset - not possiblefor me coz of the size
retrieve 1000 rows, store in temp table and create an iterator for
it and page through it - too many queries - too many inserts!!
run a query each time if someone opts page forward or backwards
Right now I am trying to get option 3 working.
I have the first page showing up as
"select * from accout order by acct fetch first 10 rows only"
Page next
"select * from account where acct>(last record) order by acct fetch
first 10 only"
page last record
"select * from account where acct=(select max(acct) from account)"
The problem is showing the previous page and i really would appreciate
help in this.
SELECT *
FROM (
SELECT
*,
ROW_NUMBER() OVER (ORDER BY acct) AS RowNum
FROM
account
) AS Data
WHERE
RowNum BETWEEN 100 AND 110;
First, you should get rid of the SELECT *. Select only the fields you need.
Place an index on acct, this will help the ROW_NUMBER() OVER (ORDER BY acct) construct.
Use a SELECT COUNT(*) FROM account to determine how many pages you will have.
Also read Fastest most/efficient way to do pagination with SQL searching DB2
The LIMIT..OFFSET solution is supported in DB2 10+.. For older versions, you have to enable the MySQL compatibility with:
$ db2set DB2_COMPATIBILITY_VECTOR=MYS
$ db2stop
$ db2start
in db2cmd in order to use that syntax.
SELECT * FROM foo LIMIT 10, 1;
try the limit and offset in mysql..
this mostly use in creating pagination
I've spent the whole day googling and deleting and inserting trying to implement this code. I've been trying to implement a reddit-like site using php and mysql. I have been following another question: PHP MYSQL Query Algorithm Help and it works very well and ranks rows according to the algorithm coded in the previous question within myphpadmin when I query a stored function
SELECT
*,
reddit_rank(`time_added`, `up_votes`, `down_votes`) as rank
FROM
`table`
ORDER BY
rank;
, but when I paste the query into my php file:
<?php
include("config.php");
$q= "SELECT *,reddit_rank(`time` , `votes_up` , `votes_down`) FROM `wallposts` ORDER BY rank LIMIT 0 , 30";
$r = mysql_query($q);
if(mysql_num_rows($r) > 0) {
while($row = mysql_fetch_assoc($r)){
...?>
It doesn't work and I get a white HTML screen. So for example in my PHP when I have
$q = "SELECT * FROM wallposts ORDER BY votes_up DESC";
my reddit/facebook-like wall has prepended each of my rows from mysql and everything works just fine. but when i change it to
$q= "SELECT *,reddit_rank(`time` , `votes_up` , `votes_down`) FROM `wallposts` ORDER BY rank LIMIT 0 , 30";
the webpage returns nothing but a white screen even though I know it works in myphpadmin.
Is there something wrong with my syntax or is it not possible to query a select all with a stored function to order the results in php?
I think I found a solution by creating a view and then querying that view instead of the original table. After I queried the stored function in myphpadmin:
SELECT
*,
reddit_rank(`time_added`, `up_votes`, `down_votes`) as rank
FROM
`table`
ORDER BY
rank;
I then created a view after those results were returned. Then instead of querying the same way in my PHP file, I queried the new mysql view with:
SELECT * FROM [view] ORDER BY rank
Voila!