mysql-query

where clause not displaying data

hi i am trying to display data based on wether data in a field is new. instead of showing only the data that is new it is showing all data. can someone point out my error. many thanks <?php include("../../js/JSON.php"); $json = new Services_JSON(); // Connect to MySQL database mysql_connect('localhost', 'root', ''); mysql_select_db(sam...

Mysql: Best practice to get last records

Hi there ! I got a spicy question about mysql... The idea here is to select the n last records from a table, filtering by a property, (possibly from another table). That simple. At this point you wanna reply : let n = 10 SELECT * FROM huge_table JOIN another_table ON another_table.id = huge_table.another_table_id AN...

SELECT 'random' results with set parameters

I am developing a system which selects questions from a database to generate a test/exam. Each question has a set 'question type' ('q_type'). I have a database of questions and need to select 4 questions, each with a different 'q_type'. The basic query to select 4 random questions at the moment is: SELECT * FROM questions ORDER BY RAN...

MySQL SUM Query

Greetings, I've got a query that I'm struggling with, this is the first time that I am encountering this type of query. I have two table as shown below. xid is the primary key in parent_tbl1, while xid is the foreign key in child_tbl2 parent_tbl1 xid pub 1 1 2 1 3 0 4 1 child_tbl2 id ttype fno xid qnty 1 ...

Help with myql query - multiple having statements not working

I'm trying to use the following query, if it only has one having statement it works as expected, however if i add the second having statement it does not work. Could someone please give me some assistance.... SELECT candidate.first_name, candidate.last_name, qualification.code, property.value AS funding_band_value, qualification...

Using Javascript to show contents that are present in DB.

Hi. I'm having the following problem, and would love some pointers in the right direction. To put things simple: I information from a table, with PHP, that I put inside a <select> tag. Whenever I click one of the options (lines) present it should load/show certain information on a cell that is right next to it. If I click another one o...

Persistent database connection for Ajax Calls

I have ajax call which pulls from the processing script 'getajax.php'. Call is made to 'getajax.php' script which has the db connection details, select, functions, etc. My question is: Everytime a call is received by 'getajax.php' it will go through mysql_connect, mysql_select, then queries. Is this the correct aproach to handle thou...

I need help writing a MySQL query correctly

I would like to pull up a catalog of games, but I also want to know if a game is in a SPECIFIC member's (current member in session) list of games already. This information is stored in a table called gameslist, which is a joiner table that joines members and games gameslists(id,memberid,gameid,rank) SELECT DISTINCT *, gameslists.memberi...

Cache results of a mysql query manually to a txt file

Hi all, Is there a way to cache results of a mysql query manually to a txt file? Ex: $a=1; $b=9; $c=0; $cache_filename = 'cached_results/'.md5("$a,$b,$c").'.txt'; if(!file_exists($cache_filename)){ $result = mysql_query("SELECT * FROM abc,def WHERE a=$a AND b=$b AND c=$c"); while($row = mysql_fetch_array($result)){ ech...

How to UPDATE the same mysql row from multiple scripts at the same time?

I am forking some php scripts and I need to update a mysql row to track some progress. I read that I should use InnoDB for this but I coulnd't find any complete example I can understand. A friend told me he uses this php code: mysql_query("SET AUTOCOMMIT=0;"); mysql_query("START TRANSACTION;"); mysql_query("SET TRANSACTION ISOLATION LEV...

Optimizing MySQL queries / database structure

I have in my MySQL database these two tables: CREATE TABLE IF NOT EXISTS `articles` ( `id` bigint(20) NOT NULL, `url` varchar(255) collate utf8_bin NOT NULL, `img` varchar(255) collate utf8_bin NOT NULL, `name` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL, `url_key` varchar(255) character set utf8 collate u...

php mysql search query wildcard problem

If I search for LENA, I only want to get results where LENA is one word and not part of a word like Lenason or Anna-Lena or Malena How do I write such a query, not "select * from users where name like '%LENA%'" What should it be instead? ...

Using SQL to compute average daily unique usage

I have a MySQL table "stats", which is a list of entries for each login into a website. Each entry has a "userId" string, a "loginTime" timestamp and other fields. There can be more than one entry for each user - one for each login that he makes. I want to write a query that will calculate the average of unique daily logins over, say, 30...

MySQL SUM Query

Hi, I've got two tables. I'm trying to calculating the SUM quantity of tbl1 tbl1.xid is the primary, while tbl2.xid is the foreign tbl1 xid pub quantity 1 1 10 2 1 2 3 0 1 4 1 5 tbl2 id ttype fno xid qnty 1 A 0 1 0 2 A 1 1 3 3 B 1 1 4 4 A ...

Should the following queries not produce the same result? (MySQL)

Assuming the following MySQL table structure, why do the two following queries produce different results? games(id) (464 records) members(id) (1 record, id=351) gameslists(id,memberid,gameid) -- (2 records, (1,351,1) and (2,351,2)) This produces null SELECT games.* FROM games INNER JOIN gameslists ON gameslists.gameid = games.id W...

Create table fail in mysql when using CURDATE() as default.

Hello, I'm trying to create the following table using phpmyadmin sql console: CREATE TABLE dates ( id int NOT NULL, id_date datetime NOT NULL DEFAULT CURDATE(), PRIMARY KEY (id) ) However I get the following error: It shows "CURDATE()" in red, so I guess that's the problem. Could anyone help me out here ? ...

COUNT and a sub COUNT in same query

Hi, I have 2 tables: members, orders. Members: MemberID, DateCreated Orders: OrderID, DateCreated, MemberID I want to find out the number of new members in a given month broken down into number of order groups, eg. 5+, 4, 3, 2, 1, 0 I've got the query to work out the number of orders for a member but how can I get these values in o...

Select adjacent records in Mysql

Assuming this table is ordered by date id | date | customer 3 | 2009-10-01| Frank 1 | 2010-10-11| Bob 4 | 2010-11-01| Mitchel 2 | 2010-11-02| Jim I would like to make a query so that knowing ID = 4 the resulting rows are $row[0]['id'] == 1 //previous $row[1]['id'] == 4 //most recent/current $row[2]['...

Mysql stored procedure error

Hello, I'm trying to add the following stored procedure to my mysql database: CREATE PROCEDURE logmsg ( _Username VARCHAR(50), _Message VARCHAR(80) ) BEGIN INSERT INTO chatlogs (sender, message) VALUES (_Username, _Message); END; But its failing the query and returning: #1064 - You have an error in your SQL syntax; check the ma...

Select Query Optimization

I have select query that fetch record based on a condition Select * from Employee where EmpStatus=#EmpStatus# The EmpStatus in the DB for each employee would either 0 or 1. EmpID EmpName EmpStatus *********************** 1 Name1 0 2 Name2 0 3 Name4 1 4 Name5 1 When I pass EmpStatus as 1, I should get list co...