I've encountered this problem for a while and I can't seem to find the right answer on google. I don't know if maybe I'm just unlucky.
Anyway, how can I get row number from a specific record I input from a PHP text field, for example:
ID NAME
11111 john
11112 roger
11113 ellis
11114 jack
11115 wendy
So if I input 11113, the output will be like "this ID is at number 3".
Here is my code:
$id=$_POST['id'];
$query="SELECT COUNT(*) from employee where id like '%$id%'";
$num=mysql_query($query);
echo "this ID is at number $num";
Can anyone point out where did I go wrong?
Here is a query example of how you can do it without adding another column for count:
SELECT e.*,b.count FROM employee as e, (SELECT COUNT(*) as count FROM employee WHERE id <= 3) as b WHERE e.id = 3
Using another select query to count all the rows with id smaller than the id requested
Or if you only want the offset of the row without its data:
SELECT COUNT(*) as count FROM employee WHERE id <= 3
At first, you should select * from the employee table
Then parse the result to array,
Scan the above array (step2) and find your value (11113)
If (3) is found, then you can return array index.
First thing:
You're using old PHP-method for database, use PDO
If you wan't to get the last insert ID, it's possible to catch it with PDO
And I don't understand your SQL, because you want to get an ID, by filtering with the ID? And when your ID is an INTEGER/NUMBER, than your statement is senseless.
Related
i need some help with my MySQL statement. i want to call the data from my MySQL table with specific condition. and i wish to present the data into a chart by using php.
For example, here is my table.
11003 trid(primary key) 1(tid) Adventure(name) US(location) 2014(date) 5(duration) 1003(id) romex(fname) jen(lname) approved(pending)
11006 trid(primary key) 1(tid) Adventure(name) US(location) 2014(date) 5(duration) 1006(id) siew(fname) siew(lname) approved(pending)
21003 trid(primary key) 2(tid) Yourfuture(name) US(location) 2014(date) 1(duration) 1003(id) romex(fname) jen(lname) approved(pending)
i want the data to count the total of people will going in each training. and list the name out.
Expected result is something like this.
Adventure romex jen
siew siew total 2
Yourfuture romes jen total 1
the pending can be 'decline' or 'approved' when approved mean they are going.
my current MySQL statement is this.
SELECT name, date, fname, lname FROM `trainingrequest` WHERE pending = 'Approved'
You can count the total people in one of two ways:
select count(*) as c from trainingrequest where pending = 'Approved'
This will return the total number of approved records. To narrow it down you will need to use an event id or such
select count(*) as c from trainingrequest where pending = 'Approved' and name = 'adventure';
or you could just grab all the data you need and count the rows in php. The first method will require two calls to the database. Counting the rows in PHP will require one call to the database!
It looks like you also want to GROUP BY your data
select * from trainingrequest where .... GROUP BY location;
I have a MySQL chart table like this : PRIMARY KEY(ID), Name, Value, Date
I need to remove duplicates if "Name AND Value AND Date" are the same as existing row.
I have beneath a solution i found while ago and which worked (not 100%), but I don't understand the command in it's total because I'm only into BASIC MySQL... Can somebody explain me a little further...
definitely what is the x at the end ???
$delDups = "delete from chart where id not in (select * from (select min(id) from chart n group by value) x)";
mysql_query($delDups);
It appears to me that you could do it simpler, like this:
$delDups = "delete from chart where id not in (select min(id) from chart n group by value)";
In the subquery you are saing:
" Hey, take all the values and find the minimun id for the group of values"
So, imagine the result of the subquery as a list, like "(12, 13, 200)".. the NOT IN operator will take that list and use it to filter the result of the upper query and say "Give me all the results, less the ones where id is in this list"
I'm not sure if I explained it as expected...
You could add an unique key for all 3 columns:
ALTER IGNORE TABLE my_table
ADD CONSTRAINT uc_unic UNIQUE (Name, Value, Date)
As to that x,mysql permit aliases,essentially name shortcuts for convenience.
What you wrote will almost work, you just want to add the name and date to the GROUP BY clause. Something like this should do.
DELETE FROM chart
WHERE id NOT IN (
SELECT MIN(id)
FROM chart
GROUP BY name, value, date)
The DELETE FROM says you want to be deleting rows from a table. The WHERE clause says which rows you actually want to delete (missing it out will remove everything). The sub-query in the brackets will look through every combination of name, value and date and give you one id back from each combination. Putting it all together, the DELETE should now drop every row whose id isn't the smallest for each group.
Hope that helps!
I have this query but it returns the name of author the number of times it exists in the database ..
$query = "SELECT bauthor FROM info WHERE Cat1 = 'novel'";
$result = MySQL_query($query);
i want the author's name to be displayed once and the number of books he has to be in a bracket ...for example author's name is aaaaa and he has written 20 books so wen i run this query it shows mw his name 20 times but i want it to be in this way aaaa(20)
I can't do much without your full schema, but try using the COUNT feature with a GROUP BY clause, like
SELECT bauthor, COUNT(books) AS numbooks FROM info WHERE ... GROUP BY bauthor
EDIT: See this SQLFiddle for an example: http://sqlfiddle.com/#!2/bb5f5/1/0
Currently I have two tables A and B. The DID is input by the user into into a input, and that is the first prompt in my program, to enter the DID.
A contains PID, DID, and MaxOccupancy. PID is professional id, DID is department id.
B contains CID, DID, and PID. CID is client ID, and DID and PID is same.
I'm outputting data from the first table into an html table which shows all that data.
However, I'm having trouble running a count on number of PID from table two. I have $countB= mysql_query ("select count (PID) from B where DID=('$_POST[DID])");
I am then writing
while($row = mysql_fetch_array($countB))
{
echo "Current occupancy:".$row['count(PID)'].;
}
Can someone help me? How do i do a where in my query for the count to get what the user put in for DID in the input box? Am i doing it wrong completely?
THANkS!
Try
$countB= mysql_query (sprintf("SELECT COUNT(PID) as total FROM B WHERE DID=('%s')",mysql_real_escape_string($_POST['DID'])));
$row = mysql_fetch_array($countB);
echo $row['total'] ;
Note .. always filter for SQL Injection
You can create an alias for the result of the COUNT which you can access in PHP:
mysql_query ("select count (PID) as PIDcount from B where DID=('$_POST[DID])");
// then reference it later
$row['PIDcount'];
The usage of $_POST[DID] in your query is subject to SQL Injection as well.
for example i have a table like this :
name rating
matei 124
andrei 20
serj 25
john 190
mike 96
andy 245
tom 73
i need to output something like this(order by rating):
john's position is 2; or, tom's position is 5; (i don't need to get all result , just one )
How can I achieve this?
Thanks in advance
Generally order of rows in a query result is not guaranteed by MySQL unless ordering is explicitly specified with ORDER BY clause. If you have some separate ordering column, you may use query like the following:
SELECT count(1) as position
FROM table
WHERE order_column <= {john's order_column value};
If you don't have ordering column, I'd recommend you to define first, what does "john's position" and "tom's position" mean.
UPDATE:
AFAIU, you want to get position in list sorted by rating (sorry, I initially did not get it). So, rating would be your order_column. In this case, you should decide, how do you calculate position, if two guys have equal rating (who's position is higher?).
So, the query may look in the following way:
SELECT count(1) as position
FROM table
WHERE
rating > (SELECT rating FROM table WHERE id={user's ID});
SELECT COUNT(*) + 1
FROM users
WHERE (rating, name) <
(
SELECT rating, name
FROM users
WHERE name = 'john'
)
Note that if you will have duplicates on both name and rating, this query will assign the same rating to both of them.
Tables are more formally known as relations in database literature - they are not guaranteed to be ordered (they are sets of "tuples"), so your question doesn't make sense. If you need to rely on an order/position, you need to define an additional column (like an auto-incrementing ID column) to capture and store that info.
Is this any help > http://craftycodeblog.com/2010/09/13/rownum-simulation-with-mysql/ ?
Would offset not work like so?
SELECT * FROM Table ORDER BY rating DESC LIMIT 1,6
This would return 1 row that has been off setted by 6 rows ? or am I mistaken, the syntax would be
SELECT * FROM Table ORDER BY rating DESC LIMIT 1 , {{POS}}