Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:22:08+00:00 2026-06-04T07:22:08+00:00

This is my query to get model information from one table and a single

  • 0

This is my query to get model information from one table and a single picture from another table. What changes do I have to make to this query in order for it to get the picture where ORDER BY sort DESC? In the table of the pictures, there is a field by the name “sort”. The default value for the field for each row is 0. But one random row has the value of 1. I want to get that particular row. I don’t, however, want to use WHERE sort=1 because then even in the case where no row has the sort value 1, one row should still get fetched.

    $sql="SELECT tc.id,tc.alias,tc.firstname,tci.imagename 
          FROM ".$pre."models tc
          INNER JOIN ".$pre."model_images tci ON tc.id=tci.userid 
          WHERE activated=1 AND sex=$sex AND city=$city AND published=1
          GROUP BY tc.id ORDER BY firstname ASC";

Thank you in advance!


Solved using:

    SELECT tc.id,tc.alias,tc.firstname, 
    (SELECT imagename FROM  ".$pre."model_images WHERE userid= tc.id 
    ORDER BY sort DESC LIMIT 1) AS imagename
    FROM ".$pre."models tc
    WHERE tc.activated=1 AND tc.sex=1 AND tc.city=2 AND tc.published=1
    ORDER BY tc.firstname ASC
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:22:09+00:00Added an answer on June 4, 2026 at 7:22 am

    You should place that in your WHERE clause aswell. One t hing to note though is to be carefull with the way you’re using the column names. It’s better to tell to which table they belong.

    So this:

    WHERE activated=1 AND sex=$sex AND city=$city AND published=1
    

    Should be:

    WHERE tc.activated=1 AND tc.sex=$sex AND tc.city=$city AND tc.published=1
    

    And then simply add the ‘sort’ column to it:

    WHERE tc.activated=1 AND tc.sex=$sex AND tc.city=$city AND tc.published=1 AND tci.sort=1
    

    If no results are returned, then make sure that there are records that meet the required conditions. Because there’s nothing wrong with the query. Try to print your query to the screen etc. to see if every variables has a value.

    edit:

    You should lose the GROUP BY.

    SELECT tc.id,tc.alias,tc.firstname,tci.imagename 
    FROM ".$pre."models tc
    INNER JOIN ".$pre."model_images tci ON tc.id=tci.userid 
    WHERE tc.activated=1 AND tc.sex=$sex AND tc.city=$city AND tc.published=1 AND tci.sort=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get the information that is returned by this query from SolrNet:
I have this query and I get error Operand should contain 1 column(s), whats
I've used this MySQL query to get data: SELECT DISTINCT A.*, B.username FROM posts
I have a problem with this query trying to get a sum and a
I need to get UserCarId(int) from this query and assign to int type variable.
I have this query, and I am trying to get the latest comment for
How do I get to make this sqsl query work: SELECT student.LASTNAME, student.FIRSTNAME, student.IDNO,
Allright, this is like thousandth time when I get kinda useless information from a
I have a model which contains a link to sql query that fetches information
I'm passing information from one page another with following php code.. Index.php page: <?php

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.