MySQL Syntax SELECT WHERE

Joined
Sep 9, 2008
Messages
582
Reaction score
12
Can someone help me, how do you write it?

Lets say, I'm done connecting with mysql.

I wan't to SELECT this_column WHERE that_column='1'

But I only want to display 1 data, the first one with that_column='1'. Only the first one it sees in the rows.

Please help, thanks..
 
Before I attempt to answer your question, let me try to educate you a bit in basic database terminology, as you seem to have trouble grasping some of it.

In a normal database - MySQL included - there are databases, table, columns, rows, cells, and values in those cells.

A database and table should be quite explanatory. A column is vertical, and represents a data type.
A row is one horizontal line of data, and contains one cell for each column in the current table.
A table's structure is defined through its columns. A table's data is defined through its rows.

I don't really understand what you mean by "display 1 data".
Nonetheless, here is a basic MySQL query:
Code:
SELECT column_name FROM table_contacts WHERE column_age = 12;
Ok, so let's break it down.
I want to get data from the table_contacts.
I want to only display column_name, located in table_contacts.
I want to only display column_name if that same row of data also has column_age equal to 12.

So, essentially, what I am going to get from this is all my contacts' names who are 12 years old. This query will return only one column, because I am only selecting column_name, but it will return one or more rows, because I want to see everyone's name if they are age 12. (WHERE column_age = 12)


Now, let's get a bit more advanced. Let's say that I want to display the name of my youngest contact. So, only one. Well, that'll look something like this:
Code:
SELECT column_name FROM table_contacts ORDER BY column_age ASC LIMIT 1;

Ok, now we have some fancy stuff at the end of this query.

ORDER BY column_age - This tells the query that it is going to order my results by age, instead of the default(usually ID, but depends on the table).

ASC - This tells the query that it is going to do the above ORDER BY using ascending. You can use either ascending or descending (ASC or DESC). Ascending is essentially the oldest(integer/datetime)/the smallest(integer)/the earliest(datetime). Descending is the opposite - it sorts by the newest(datetime)/the biggest(integer).

LIMIT 1 - This tells the query that it is only going to return one result. Even if there are more than one results available, it will only return one (the first that it finds).


I hope this helps clear some stuff up.
 
Thanks,
LIMIT 1 - This tells the query that it is only going to return one result. Even if there are more than one results available, it will only return one (the first that it finds).

Thats, what I was looking for ^_^

---------- Post added at 07:02 PM ---------- Previous post was at 06:54 PM ----------

One question, I have a textfile that contains 1000 coupon keys.
a coupon key per newline

I need to insert all those into a mysql database, is there an automated way to do that?
 
Thanks ^_^ got it now.. Next problem is why is this code not updating?

mysql_query("UPDATE coupon_data SET status='1' WHERE key='0001-RW02-S19R-PP76'");

When I change the column "key" to "id" and use the id instead it updates the column status.

But when I try using the key that is storing in VarChar(19), nothing happens..?hmmm
 
Thanks ^_^ got it now.. Next problem is why is this code not updating?

mysql_query("UPDATE coupon_data SET status='1' WHERE key='0001-RW02-S19R-PP76'");

When I change the column "key" to "id" and use the id instead it updates the column status.

But when I try using the key that is storing in VarChar(19), nothing happens..?hmmm

KEY is a reserved word in MySQL, that could be your problem.

Use `` to solve this conflict.
Code:
WHERE `key`='0001-RW02-S19R-PP76'
 
Back