A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearPHP/Mysql Columns imageid, catid, imagedate, userid - page 1
User InfoPosts
PHP/Mysql Columns imageid, catid, imagedate, userid#1
I have just started to learn PHP/Mysql and up until now have only been doing some pretty basic querys but am now stumped on how to do something.

Table A
Columns imageid,catid,imagedate,userid

What I have been trying to do is get data from Table A sorted by imagedate. I would only like to return 1 result (imageid,userid) for each catid. Is there a way to check for uniqueness in the mysql query?

Thanks
John

posted date: 2009-04-09 12:48:00


Re: PHP/Mysql Columns imageid, catid, imagedate, userid#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-09 12:48:01


Re: PHP/Mysql Columns imageid, catid, imagedate, userid#3
Lookup the word DISTINCT.

posted date: 2009-04-09 12:50:00


Re: PHP/Mysql Columns imageid, catid, imagedate, userid#4
Yes you can use the DISTINCT option.select DISTINCT imageid,userid from Table A WHERE catid = XXXX

posted date: 2009-04-09 12:51:00


Re: PHP/Mysql Columns imageid, catid, imagedate, userid#5
SELECT DISTINCT `IMAGEID`, `USERID` FROM `TABLEA`ORDER BY `IMAGEDATE`; UPDATE `USER` SET `reputation`=(SELECT `reputation` FROM `user` WHERE `username`="Jon Skeet")+1 WHERE `username`="MasterPeter"; //in your face, Jon ;) hahaha ;P

posted date: 2009-04-09 12:56:00


Re: PHP/Mysql Columns imageid, catid, imagedate, userid#6
didn't work but was worth a try :D ;P

posted date: 2009-04-09 12:58:00


Re: PHP/Mysql Columns imageid, catid, imagedate, userid#7
What a hack attempt, at least it is funny.

posted date: 2009-04-09 13:14:00


Re: PHP/Mysql Columns imageid, catid, imagedate, userid#8
Hope you don't mind my stealing your solution. Your answer will be selected, I am sure of that :) This question didn't exactly require Jon Skeet's skill but I sure hope he comes to check the answers :) :D

posted date: 2009-04-09 13:32:00


Re: PHP/Mysql Columns imageid, catid, imagedate, userid#9
If you want to check for uniqueness in the query (perhaps to ensure that something isn(t duplicated), you can include a WHERE clause using the MySQL COUNT() function. E.g.,SELECT ImageID, UserID FROM TABLEA WHERE COUNT(ImageID) < 2.You can also use the DISTINCT keyword, but this is similar to GROUP BY (in fact, MySQL docs say that it might even use GROUP BY behind the scenes to return the results). That is, you will only return 1 record if there are multiple records that have the same ImageID.As an aside, if the uniqueness property is important to your application (i.e. you don(t want multiple records with the same value for a field, e.g. email), you can define the UNIQUE constraint on a table. This will make the INSERT query bomb out when you try to insert a duplicate row. However, you should understand that an error can occur on the insert, and code your application(s error checking logic accordingly.

posted date: 2009-04-09 16:38:00


Re: PHP/Mysql Columns imageid, catid, imagedate, userid#10
Please write a more meaningful title

posted date: 2009-04-09 19:33:00


Re: PHP/Mysql Columns imageid, catid, imagedate, userid#11
Very nicely explained for us noobs

posted date: 2013-12-10 16:39:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.