i have one table where one column name is city. for every user i can display how many number of users are there from that city (from which user belongs) but i want to display who all are . so i have tried this mysql query but it is not working
$res = sql_query("select * from ".tb()."data where city='{$owner['city']}' group by city order by fullname limit 10 " );
if any one is having any idea please advice how to solve.the main work is to show all users from that city suppose if user A belongs to Tokyo so when someone will click on that link which shows how many numbers are there from Tokyo it should show all users from that country .other part i have done and i am able to display also but it displays all results .
Remove the group by from the request:
SELECT * FROM data WHERE city='Tokyo' ORDER BY fullname;
As per your comments just use the following query :
$res = sql_query("select * from ".tb()."data where city='{$owner['city']}' order by fullname " );
Related
Help with an SQL Query:
I want to get the last message of every conversation between client and helper. Based on same clientId lets say clientId = 1 but different helperId. What I am trying to achieve is build an inbox where last only last message of the conversation is available.
Here is the table structure.
Note: The clientId is known in the table everyother field is not known.
Please have a look at the table structure below:
You can do it this way:
$table = // table name
$clientId = // client's id
$query = "select * from ".$table." where `clientId`= '".$clientId."' group by `helperId` order by `id` desc";
This would give you the last conversation for the same client but with different helper.
I have this existing sql query that is working quite fine. It searches in a table called qu_pap_users under a particular field (userid) and then randomly grabs a userid to copy into a different table called settings...
$sql = "REPLACE INTO qu_g_settings (settingid, name, value)
SELECT '9038fa14', 'assignNonReferredAffiliateTo', qu_pap_users.userid
FROM qu_pap_users ORDER BY RAND() LIMIT 1";
I would like to change the last part of the query so that it does the following:
Locate the userid field as above, but now it needs to look into an additional field called data12.
I want it to only copy userids where the data12 field = Affiliate Distributor Licensed
After it has found all userids with a data12 field that = Affiliate Distributor Licensed, I would like it to then randomly select from those userids only.
I hope this is clear enough. Thank you in advance...
Try this, You have to just add where clause to filter data12 field = Affiliate Distributor Licensed
$sql = "REPLACE INTO qu_g_settings (settingid, name, value)
SELECT '9038fa14', 'assignNonReferredAffiliateTo',
qu_pap_users.userid
FROM qu_pap_users
WHERE qu_pap_users.data12 = 'Affiliate Distributor Licensed'
ORDER BY RAND() LIMIT 1";
First of all, let me tell you that I am pretty novice at PHP & MySQL and trying to learn. So I created a webapp where a random apartment number is to be shown against a name of person ordered by membership number seniority. The apartment number and name of persons come from separate tables.
I was able to get a random name by implementing the query like this-
SELECT bd, rank, name FROM person ORDER BY RAND() DESC LIMIT 1
But what I need is - when I submit the form it will show the most senior person's name and a random apartment number against him, when I click to submit the form again then the next senior person and a random apartment number and so on. In that case what my query should be?
My code looks like this-
if (isset($_POST['submit'])) {
$query = "SELECT house_no FROM houses ORDER BY RAND() LIMIT 1";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result))
{
global $won;
$won = $row['house_no'];
echo $won;
}
$query2 = "SELECT bd, rank, name FROM person ORDER BY RAND() DESC LIMIT 1";
$result2 = mysqli_query($link, $query2);
while ($row2 = mysqli_fetch_array($result2))
{
$winner_rank = $row2['rank'];
$winner = $row2['name'];
$winner_bd = $row2['bd'];
global $winner, $winner_bd, $winner_rank;
echo $row2['rank']. " ";
echo $row2['name']. ", ";
echo "Member No/".$row2['bd'];
} }
I can help, but i don't think i fully understand what you are trying to accomplish with the code. Try commenting each step of the code and moreover i don't know what the structure of the database is like.
But from what i understand ==> when I submit the form it will show the most senior person's name and a random apartment number against him, when I click to submit the form again then the next senior person and a random apartment number and so on.
//to make sure an apartment is not issued to a member twice.
i suggest that you have a column called assigned in both users and apartment tables that will have a default value of 0
so, when you select any value for user and apartment, change the value of that column to 1.
//for the senior member
//I assume you have a users table :)
I think the most senior person will have the lowest id number in the db.
Select all the members that as a value of 0 in the assigned column then order by the id, limit result to 1
then save that value in a variable.
//for random apartment
//I assume you have a table for apartments
Just select the id and apartment number that has an assigned valued of 0 order by rand and limit to 1 then save it in a variable.
//echo the variables
I have this query
$r=mysql_query(
"SELECT *
FROM advertisements
WHERE
$filter exposure!='0' AND `status`='2' AND
(clicks_left_micro>0 OR clicks_left_mini>0
OR clicks_left_standard>0 OR clicks_left_extended>0
OR fixed='1')
ORDER BY
exposure DESC, fixed DESC"
);
In my advertisements table I have a column called geoFilter. That column can either be empty or it can contain values (country codes) like:
DK, US, CA, EN,
I get the users country code like this (stored in DB):
$userdata['country_code'];
My question is, how can I select from the advertisements table where users country code is present?
You can try to start your WHERE clause with: WHERE country_code LIKE „%US%“ AND (…) to get only results where US is contained in the country_code column.
I have a messaging system (very basic) that has a table like this:
**MESSAGE_ID** **RUSER_ID** **SUSER_ID** **MESSAGE_DATA** **DATE**
RUSER is the receiving user, and SUSER is the sending user. If I wanted to output a query that would output a certain users messages, I would currently do:
Select * from PRIVATE_MESG where RUSER_ID=$USER_ID or SUSER_ID=$USER_ID
That would give me all message_id's that are associated with that USER_ID. What I would like, is to create a column that would produce only the ID associated with RUSER_ID or SUSER_ID associated with a specific user. I need it to choose the messages that RUSER_ID or SUSER_ID are equal to a USER_ID but only display the one that isn't USER_ID
I would then like to do a group by the output of that query.
Any help is greatly appreciated!
Thanks!
update I am not really looking for a message_id, I am just looking for a list of users who that person has written to or received from.
UPDATE
Just so everyone knows, I recieved the answer to this question perfectly! I tweaked it later on so that it would also display them by date from newest to oldest. I did this by spliting the DATETIME into DATE and TIME USING the DATE() and TIME() Function. Here was my final query:
SELECT
IF(RUSER_ID = $USER, SUSER_ID, RUSER_ID) as THE_OTHER_GUY, DATE(DATE) as DAY, TIME(DATE) as TIME
FROM PRIVATE_MESG
WHERE RUSER_ID = $USER
OR SUSER_ID = $USER;
group by THE_OTHER_GUY ORDER BY DAY DESC, TIME DESC
Hope this helps the next person!
You can query:
SELECT
*,
IF(RUSER_ID = $USER_ID, SUSER_ID, RUSER_ID) as THE_OTHER_GUY
FROM PRIVATE_MESG
WHERE RUSER_ID = $USER_ID
OR SUSER_ID = $USER_ID;
SELECT SUSER_ID FROM PRIVATE_MESG WHERE RUSER_ID=$USER_ID
UNION
SELECT RUSER_ID FROM PRIVATE_MESG WHERE SUSER_ID=$USER_ID
It retrieves:
- the list of user IDs who sent messages to $USER_ID
- the list of user IDs who received messages from $USER_ID
And UNION groups the 2 lists in a single result set.