-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
If you are not storing dates in yyyy-mm-dd format then you can't. Dates in other formats do not compare correctly.
-
to sort add ORDER BY AvgFuel to the end of the query To pull in data from another table, use a JOIN http://www.phpfreaks.com/tutorial/data-joins-unions
- 3 replies
-
- sql
- calculation
-
(and 1 more)
Tagged with:
-
Do the print_r() after you have built the array, not every time you add an element. while($row=mysql_fetch_assoc($result)){ $riders[] = $row['rider_name']; } echo '<pre>', print_r ($riders, 1), '</pre>'; // output is easier to read this way And you should use mysqli functions, not mysql, or you will soon be in for a lot of rewriting when the mysql functions are no longer available
-
... and in this case you need SUM and not COUNT SELECT COUNT( `points` ) AS `rides` , SUM( `points` ) AS `pts` , SUM(IF(`points`=3,1,0)) AS `wins` , SUM(IF(`points`=2,1,0)) AS `second` , SUM(IF(`points`=1,1,0)) AS `third` , SUM(IF(`points`=0,1,0)) AS `unplaced` , rider_name FROM tbl_heat WHERE card_id = $card GROUP BY `rider_name` ORDER BY pts DESC LIMIT 8
-
How to fill a datetime field from another field which is int(11) datetime?
Barand replied to nkamp's topic in MySQL Help
It depends on how the numeric date is stored . Is it 20140502, or 1398985200 (unix time) This example shows how to convert both (numdate and unixtime are INT, the realdate1/2 are DATETIME) mysql> SELECT * FROM test.datetest; +----+----------+------------+---------------------+---------------------+ | id | numdate | unixtime | realdate1 | realdate2 | +----+----------+------------+---------------------+---------------------+ | 6 | 20140502 | 1398985200 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | +----+----------+------------+---------------------+---------------------+ UPDATE datetest SET realdate1 = numdate , realdate2 = FROM_UNIXTIME(unixtime); +----+----------+------------+---------------------+---------------------+ | id | numdate | unixtime | realdate1 | realdate2 | +----+----------+------------+---------------------+---------------------+ | 6 | 20140502 | 1398985200 | 2014-05-02 00:00:00 | 2014-05-02 00:00:00 | +----+----------+------------+---------------------+---------------------+ -
If you don't want to update a field then don't put it in the update query
-
@Psycho Strange - it worked for me mysql> SELECT * FROM books; +---------+--------+------------+------------+ | book_id | car_id | start_date | end_date | +---------+--------+------------+------------+ | 1 | 1 | 2014-05-03 | 2014-05-07 | | 2 | 1 | 2014-05-08 | 2014-05-11 | | 3 | 2 | 2014-05-06 | 2014-05-08 | | 4 | 3 | 2014-05-03 | 2014-05-12 | | 5 | 4 | 2014-05-03 | 2014-05-04 | | 6 | 4 | 2014-05-11 | 2014-05-15 | +---------+--------+------------+------------+ ID 4 is the only one not booked between May 5 and May 10 mysql> SELECT car.id, car.model -> FROM car -> LEFT JOIN books ON books.car_id = car.id -> AND books.start_date <= '2014-05-10' -> AND books.end_date >= '2014-05-05' -> WHERE books.car_id IS NULL -> GROUP BY car.id; +----+----------+ | id | model | +----+----------+ | 4 | Fiat 500 | +----+----------+
-
try SELECT car.id, car.model, books.id, books.car_id, books.start_date, books.end_date FROM car LEFT JOIN books ON books.car_id = car.id AND books.start_date <= '{$_POST['end']}' AND books.end_date >= '{$_POST['start']}' WHERE books.car_id IS NULL GROUP BY books.car_id
-
To be honest I have never benchmarked it. But I wanted to show where OP's code should be calling the date() function and on which value. Your code would also need a different alias other than "bday" or it won't sort correctly
-
The character immediately before the second word needs to be whitespace for ucwords() to work $str = "this\nafternoon"; echo nl2br(ucwords($str)); => This Afternoon $str = "this<br>afternoon"; echo ucwords($str); => This afternoon
-
$result = mysql_query("SELECT * FROM `friend` ORDER BY bday") or die($myQuery."<br/><br/>".mysql_error()); while($row = mysql_fetch_array($result)) { $program = $row['ID']; echo "<h2>" . $row['firstname'] . "</h2>"; echo "<h3>" . date('m-d-Y', strtotime($row['bday'])) . "</h3>"; }
-
try $xml = simplexml_load_file('test.xml'); foreach ($xml->xpath("//Line[@Status='Active']") as $line) { echo $line['Id']; } edit : Almost forgot - first you need valid XML. Those Line tags need to end with "/>"
-
Sounds like an error somewhere in your query. What does echo $conn->error; tell you?
-
mysqli_query($con, "INSTER INTO `my_build` (serial_no, description, price) VALUES ('$s', '$d', '$p')"); Use echo $con->error; to check for errors
-
Have you considered looking at the output from mysql_error() and letting that tell you what's wrong?
-
Join the home table with the category table using a CROSS JOIN (cartesian join) so you get a row for every category/home value. Then do a LEFT join with the link table to see which match SELECT home.id as homeid , cat.id as catid , cat.name , IF(l.category_p_id IS NULL, '', 'checked') as checked FROM home CROSS JOIN category_p cat LEFT JOIN link_category_p_home l ON home.id = l.home_id AND cat.id = l.category_p_id WHERE home.id = 3 ORDER BY cat.id Gives +--------+-------+--------+---------+ | homeid | catid | name | checked | +--------+-------+--------+---------+ | 3 | 1 | Cat 1 | | | 3 | 2 | Cat 2 | checked | | 3 | 3 | Cat 3 | | | 3 | 4 | Cat 4 | checked | | 3 | 5 | Cat 5 | checked | | 3 | 6 | Cat 6 | | | 3 | 7 | Cat 7 | | | 3 | 8 | Cat 8 | checked | | 3 | 9 | Cat 9 | checked | | 3 | 10 | Cat 10 | | +--------+-------+--------+---------+
-
If "table" really is the name of your table I suggest you change it as "table" is a reserved word in SQL. If you must use that name you have to put it in backticks "SELECT * FROM `table` " and avoid using "SELECT *", specify the columns you want to select
-
You should not have multiple occurences of the word "Brisbane" (or any other city name). "Brisbane" should be stored once in a location table and the id of that record should be in your activepropertylist table. In other words, your data should be normalized. EG +------------+ | Country | +------------+ | countryid |---+ | countryname| | +------------+ | | +------------+ | | Region | | +------------+ | | regionid |--+ | | regionname | | +---<| countryid | | +------------+ | | +---------------+ | | Location | | +---------------+ | | locationid |--+ | | locationname | | +--<| regionid | | +---------------+ | | | +-----------------+ | | activeproperty | | +-----------------+ | | propertyid | +---<| locationid | | propertytype | | etc. | +-----------------+
-
I wouldn't advocate GROUP_CONCAT for fields like comments which can be fairly lengthy. GROUP_CONCAT has a 1024 character limit and you could easily get unknown truncation of the output with long data fields.
-
Have indexes on Location, City, CountryName, Region and use "=" instead of LIKE so it can use those indexes. Better still, use IDs for location, city etc in the search, where the user can select those IDs from dropdowns, to avoid spelling errors in the user input of search terms.
-
You need to specify the join condition between getpriceproducts and getpriceretailer tables, which is probably the retailerid. If you don't you join every record in one table with every record in the the other, hence the huge output It is also more efficient to use an explicit JOIN statement. instead of "..FROM A, B.." use ... FROM A INNER JOIN B ON A.x = B.y
-
Use quotes round the string value or SQL thinks it is a column name UPDATE emails SET joined = 1, id = 80 WHERE referral_code = 'fa4cc5218dc647ee7345c84103938812' AND mem_id = 26
-
DATE() extracts the date portion from a DATETIME field. DAY() extracts the day of the month http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html
-
Try SELECT DATE(date) as date, COUNT(*) AS graphcount FROM `reviews` GROUP BY DATE(date) ORDER BY date ASC