I have two tables, users and sales. sales.userId = user.id
I am trying to use following query. To search parameters in both users and sales.
SELECT * FROM sales INNER JOIN users ON users.id = sales.userId WHERE 'users.fullname' like '%Cloud%' OR 'users.storename' like '%cloud%' order by sales.id DESC
No result is showing up. What could be wrong in query.
e.g one user['fullname'] is CloudBuck Abc
if(isset($_GET["q"]) && $_GET["q"]!="")
{
$q = trim($_GET["q"]);
$where = "WHERE users.fullname like '%".$q."%' OR users.storename like '%".$q."%'";
}
$query = "SELECT *
FROM sales
INNER JOIN users ON users.id = sales.userId ".$where." order by sales.id DESC";
You are quoting the field names and this is not allowed, either use backticks if its a reserved word or do not use it.
SELECT * FROM sales
INNER JOIN users ON users.id = sales.userId
WHERE `users`.`fullname` like '%Cloud%'
OR `users`.`storename` like '%cloud%'
order by sales.id DESC
SELECT * FROM sales INNER JOIN users ON users.id = sales.userId
WHERE (users.fullname like '%Cloud%' OR users.storename like '%cloud%')
order by sales.id DESC
Remove the single qoutes around the column names:
SELECT *
FROM sales
INNER JOIN users ON users.id = sales.userId
WHERE users.fullname like '%Cloud%' OR users.storename like '%cloud%' order by sales.id DESC
Because mysql will use it as string if the single quote is set.
Related
My tables
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY id DESC
LIMIT 1";
This is my SQL query, my task is to show the last records from 3 tables, but the table is blank, I don't know why,thanks in advance people :)
I guess the problem is coming from the ORDER BY id DESC .
Indeed, you have no column so called id.
You should probably remove this clause, in order to make your code work.
If you want to take the last records anyway, you can put an ORDER BY address_id DESC which will do the job !
The code directly edited :
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY adress_id DESC
LIMIT 1";
This may work:
SELECT a.address_id, u.user_id, n.note_id
FROM addresses a
LEFT JOIN users_addresses ua ON ua.ua_address_id = a.address_id
LEFT JOIN users u ON u.user_id = ua.ua_user_id
LEFT JOIN notes n ON n.note_user_id = u.user_id
ORDER BY a.address_id DESC
LIMIT 1
Here is the query to get all data from all the tables, not sure what do you mean last records from 3 tables, I can see four tables there:
SELECT *
FROM `addresses`
LEFT JOIN `users_addresses` ON `users_addresses`.`ua_address_id` = `addresses`.`address_id`
LEFT JOIN `users` ON `users`.`user_id` = `users_addresses`.`ua_user_id`
LEFT JOIN `notes` ON `notes`.`note_user_id` = `users`.`user_id`;
I have this query:
$q = "SELECT * FROM user
WHERE sec='1' AND reg_by='".$_SESSION['login_username']."'
ORDER BY date DESC LIMIT $startrow, 30 ";
I have another table which stores appointments, it has a column named meet.
How can I sort this query by meet?
Not all data at users are in other table.
You can use the below query. Replace another_table with your original table name :
$q = "SELECT u.* FROM user AS u LEFT JOIN another_table AS at ON u.userid = at.userid WHERE u.sec='1' AND u.reg_by='".$_SESSION['login_username']."' ORDER BY at.meet DESC LIMIT $startrow, 30 ";
you can use joining for this like
select user.*,meet.* from user left join meet on (meet.userid = user.id) where user.sec='1' AND user.reg_by='".$_SESSION['login_username']."' order by meet.userid DESC
$q = "SELECT * FROM user INNER JOIN user
ON meets.userid=user.userid WHERE sec='1' AND reg_by='".$_SESSION['login_username']."' ORDER BY date DESC LIMIT $startrow, 30 ";
I have tables:
trips: Trip_Num, Trip_Type,Trip_Geographic, etc.
travelers_trips_history: Username, Trip_Num, Trip_Type, Trip_Season, etc.
In table 2 there are trips the user did and I need to know what is the most common Trip_Geographic that in his trip's history (The Trip_Geographic need to be known by table 1 - trips by his Trip_Num).
I am doing a project in PHP and need to do a SQL query for a specific user (by his username) - need to receive a table that the columns are: 1. Trip_Geographic, count of the occurrence the Trip_Geographic is in the travelers_trips_history.
Attached the tables I have:
trips
travelers_trips_history
The query I did:
$query = "select traveler_trips_history.*, COUNT(trips.Trip_Geographic) AS Trip_Geographic_occurrence
FROM traveler_trips_history INNER JOIN trips ON
travelers_trips_history.Trip_Num = trips.Trip_Num
GROUP BY Trip_Geographic
ORDER BY Trip_Geographic_occurrence DESC
WHERE traveler_trips_history.Traveler_Username = $username";
I receive a error about the Group BY (check syntax), however, I am not sure the query is going to do what it should do.
Try this
SELECT traveler_trips_history.*
,COUNT(trips.Trip_Geographic) AS Trip_Geographic_occurrence
FROM traveler_trips_history
INNER JOIN trips ON travelers_trips_history.Trip_Num = trips.Trip_Num
WHERE traveler_trips_history.Traveler_Username = $USERNAME
GROUP BY traveler_trips_history.travelers_trips_history
,traveler_trips_history.Username
,traveler_trips_history.Trip_Num
,traveler_trips_history.Trip_Type
,traveler_trips_history.Trip_Season
ORDER BY Trip_Geographic_occurrence DESC
You have 2 issues in your query:
1- You should have the where clause before the GroupBy Clause
2- If you GroupBy Col1, you can only select Col1 or use Aggregate Functions on another Columns (SUM, AVG, etc...)
You need to check the Rules for GroupBy and understand it better.
Edit after your comment:
$query = "select Trip_Geographic, COUNT(*) AS Trip_Geographic_occurrence
FROM traveler_trips_history INNER JOIN trips ON
travelers_trips_history.Trip_Num = trips.Trip_Num
WHERE traveler_trips_history.Traveler_Username = $username
GROUP BY Trip_Geographic
ORDER BY Trip_Geographic_occurrence DESC";
Try like this
The problem is your where clause and group by columns.
SELECT tr.Trip_Geographic
,COUNT(hs.*) AS Trip_Geographic_occurrence
FROM traveler_trips_history hs
INNER JOIN trips tr ON hs.Trip_Num = tr.Trip_Num
WHERE hs.Username = $USERNAME
GROUP BY tr.Trip_Geographic
ORDER BY Trip_Geographic_occurrence DESC
Your problem is Haing WHERE clause after GROUP By and using columns that are not in GROUPED BY or AGGREGATE functions.
Try the following which has the username, trip_geographic and the no of trips.
select traveler_trips_history.Traveler_Username, traveler_trips_history.Trip_Geographic, COUNT(trips.Trip_Geographic) AS Trip_Geographic_occurrence
FROM traveler_trips_history INNER JOIN trips ON
travelers_trips_history.Trip_Num = trips.Trip_Num
GROUP BY Trip_Geographic, traveler_trips_history.Traveler_Username
having traveler_trips_history.Traveler_Username = $username
ORDER BY Trip_Geographic_occurrence DESC
$sql = "select body, stamp from posts where user_id = '$userid' order by stamp desc";
NOTE: the above query works fine. What I want to do is also select username from my users table, and display the username that matches the user_id.
I have edited the above statement like so, and it doesnt work. Can someone suggest the correct query? My goal is to also be able to display usernames. in addition to simply displaying user_id.
$sql = "select body, stamp from posts AND username from users where user_id = '$userid' order by stamp desc";
My goal is to also be able to display usernames. instead of simply user_id.
You'll need to use a JOIN to bring the two tables together on the matching field, so something like:
$sql = "SELECT p.body, p.stamp, u.username FROM posts p INNER JOIN users u ON p.user_id=u.user_id WHERE p.user_id='$userid' ORDER BY p.stamp DESC";
You can use table name to select the columns. Ex:
$Query = "select table1.body, table1.stamp, users.username from posts, users where user_id = '$userid' order by stamp desc";
But, this method is not good in performance.
The best method is:
$Query = "SELECT table1.body, table1.stamp, users.username
FROM posts
INNER/LEFT/RIGHT JOIN users
ON users.user_id = '$userid' AND users.user_stamp = stamp.stamp_id
All the tables must be related.
Greetings,
$sql = "select body, stamp from posts , username from users where user_id = '$userid' order by stamp desc";
I just corrected the syntactical error in your query... that is the AND keyword should be replaced by ,.
$sql = "
select body, stamp from posts where user_id = '$userid' order by stamp desc
UNION ALL
select body, stamp from username where user_id = '$userid' order by stamp desc
";
http://dev.mysql.com/doc/refman/5.0/en/union.html
You should use aliases or table name to avoid the duplicate problem like this
tablename.column
or
alias.column
you can set the alias in your where clause :
FROM table as alias_name
$sql = "SELECT p.body,p.stamp,u.username from posts p LEFT JOIN users as u ON (p.user_id = u.user_id) WHERE user_id = '$user_id' ORDER BY p.stamp DESC";
should do it
Try this:
select body, stamp, username
from posts p JOIN users a ON p.user_id = a.user_id
WHERE p.user_id = '$userid' order by stamp desc
This should work fine:
SELECT posts.body, posts.stamp, users.username
FROM posts, users
WHERE posts.user_id = '$userid' AND posts.user_id = users.user_id
ORDER BY posts.stamp DESC
select body, stamp, username
from posts,users
where users.user_id = post.user_id
and users.user_id = '$userid'
order by stamp desc;
I need to alter my existing JOIN query below to also include the data from users.image correlating to the UserID of the post maker. Something like:
users.image WHERE users.UserID = posts.userid
I am not very good with join queries yet. How would I do this?
Existing Query:
$result = mysql_query("SELECT posts.* FROM listen JOIN posts ON posts.userid = listen.listenid WHERE listen.userid = '$user_id' ORDER BY DATE desc") or die(mysql_error());
Just add another JOIN clause:
SELECT posts.*
FROM listen
JOIN posts ON (posts.userid = listen.listenid)
JOIN users ON (users.UserID = posts.userid)
WHERE listen.userid = '$user_id'
ORDER BY DATE desc
You may need to change the JOIN to a specific join such as LEFT JOIN, depending on what you're after.
Btw, it is easier to see the query on multiple lines.
Edit: You'll probably want to add additional items that you are selecting with your fields, such as SELECT posts.*, users.*