Web Analytics Made Easy -
StatCounter MySQL query syntax - CodingForum

Announcement

Collapse
No announcement yet.

MySQL query syntax

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • MySQL query syntax

    Hello:

    I have a problem which I just can't solve. Let me explain what I intend to do.

    Let's say I have a table which has the fields: id,image,text. In this fields the only one which can be null is image.

    What I want to do is retreive the content of my table in this way:

    image
    no-image
    image
    no-image
    ...

    I want to alternate the image and no image, but I don't know how to do it, I tried with case without success. I hope someone can give me some pointers.

    THANKS

  • #2
    I don't realy understand it. Do you want to alternate the records inside the recordset, on the value of 'image' ? So a record where it is Null and then one where there is a value etc?

    If so, i don't think this is possible in SQL. Probably possible with some server side scripting, where you 'interleaf' two recordsets (1 with the records with images=Null and one whit the other records)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

    Comment


    • #3
      Do you want to alternate the records inside the recordset, on the value of 'image' ? So a record where it is Null and then one where there is a value etc?
      Exactly that is what I want to do.

      Comment


      • #4
        Hmm. Do you have the same number of Null-records as records with values?

        One possible option would be to get the number ofrecordsfromthe biggest recordset,and then use for loop to run through both of them. With mysql_data_seek,you can place the pointer at the $i'd record andso jump one row down at a time in each recordset.

        Then it's just a matter of printing out the record that you want.

        If you reach the end of the smallest redcordset, the the interleaving will stopand you'll et the records from the biggest recordset directly after eachother.

        PHP Code:
        $select1"SELECT ... WHERE images = Null";
        $result1=mysql_query($select1) or die ('Queryproblem Null-records');
        $top mysql_num_rows($result1);
         
        $select2"SELECT ... WHERE images != Null";
        $result2=mysql_query($select2) or die ('Queryproblem not-Null-records');
        if (
        $top mysql_num_rows($result2)){
         
        $top=mysql_num_rows($result2);
        }

        for (
        $i=0;$i<$top;$i ++){
          if(
        mysql_data_seek($result1,$i)){ //returns true if there is such a row in therecordset. If the pointer is EOF, then it will skip back to the other array
             
        $rownull=mysql_fetch_assoc($result1);
              echo  (
        $rownull['yourvariable'] ...);
          }
          if(
        mysql_data_seek($result2,$i)){ //returns true if there is such a row in therecordset. If the pointer is EOF, then it will skip back to the other array
             
        $rownotnull=mysql_fetch_assoc($result2);
              echo  (
        $rownotnull['yourvariable'] ...);
          }

        Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

        Comment

        Working...
        X