I am selecting the user's information from my MySQL database as shown below:
SELECT * FROM `Users` WHERE `$user_or_id` = ?
However, I would like to add an extra bit off information to the returned data. The extra bit of data is the total number of records in a table named 'Venues' where the rows' field, 'user_id' is the same as the 'id' field in the table, 'Users'.
Please can you tell me where I am going wrong with the following query? Here is the error I am receiving:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '*) FROM Users AS u INNER JOIN Venues AS v ON u.id =
v.user_id WHERE u.id = '' at line 1
SELECT u.*, v.count(*) FROM `Users` AS u INNER JOIN `Venues` AS v ON u.id = v.user_id WHERE u.$user_or_id = ?
SELECT u.*, COUNT(v.*) FROM `Users` AS u INNER JOIN `Venues` AS v ON u.id = v.user_id WHERE u.$user_or_id = ?
COUNT is a MySQL function, not a member of table v. Pass it an argument representing what you want to count-- in this case, v's rows.
It should be COUNT(v.*). Otherwise it's interpreted as "function count inside table V", which isn't valid.
Just use count(*) instead of v.count(*).
Related
I have two Tables - char_items and items. item_id is a common field among the two tables.
I want read the item_id from 'char_items' table and use that to obtain other information from the 'items' table based on that item_id. But my query is showing up as incorrect in MySQL. Please help --
SELECT * FROM `char_items` WHERE char_id=$char_id && isSlotted=1 INNER JOIN `items` ON char_items.item_id=items.item_id
I keep getting the message:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN `items` ON char_items.item_id=items.item_id
LIMIT 0, 30' at line 1
Joins need to happen before the where clause
SELECT *
FROM char_items c
INNER
JOIN items i
ON c.item_id = i.item_id
WHERE char_id = $char_id
AND isSlotted = 1
where should be after join clause as like below.
SELECT * FROM `char_items` INNER JOIN `items` ON char_items.item_id=items.item_id WHERE char_id=$char_id && isSlotted=1;
I was using this:
SELECT res.*, rac.*, u.*, t.*, c.*
FROM Results res
INNER JOIN Races rac USING (RaceID)
INNER JOIN Users u USING (UserID)
INNER JOIN Teams t USING (TeamID)
INNER JOIN Cars c USING (CarID)
WHERE res.SeasonNumber = '$SeasonNumber' AND res.LeagueID = '$LeagueID' AND Position = '1' AND ResultConfirmed = '1'
ORDER BY Position ASC
Which works fine, but I've since realised I need to have CarID added in to Results table, but when I add it in, it gives me the error that the field is ambiguous. What I'd like to do is get the Car name from Cars table where CarID joins Cars and Results. When I try to do this though:
SELECT res.*, rac.*, u.*, t.*, c.*
FROM Results res
INNER JOIN Races rac USING (RaceID)
INNER JOIN Users u USING (UserID)
INNER JOIN Teams t USING (TeamID)
INNER JOIN Cars c USING (res.CarID)
WHERE res.SeasonNumber = '$SeasonNumber' AND res.LeagueID = '$LeagueID' AND Position = '1' AND ResultConfirmed = '1'
ORDER BY Position ASC
I get the following error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '.CarID) WHERE res.SeasonNumber = '1' AND res.LeagueID = '1' AND
Position = '1' ' at line 6
You can replace your USING clause with ON(),in USING() clause i guess you add the columns name that are same in other table you are joining but you placed the join in last and using alias res mysql won't allow this
INNER JOIN Cars c ON(res.CarID =c.CarID)
If you need to use USING() clause you need to adjust the join placements like
SELECT res.*, rac.*, u.*, t.*, c.*
FROM
Cars c
INNER JOIN Results res USING (CarID)
INNER JOIN Races rac USING (RaceID)
INNER JOIN Users u USING (UserID)
INNER JOIN Teams t USING (TeamID)
WHERE res.SeasonNumber = '$SeasonNumber' AND res.LeagueID = '$LeagueID' AND Position = '1' AND ResultConfirmed = '1'
ORDER BY Position ASC
But ON() clause is more readable form
I've build the following query:
(SELECT
privatemsgs.id,
privatemsgs.useradn,
privatemsgs.useraid,
privatemsgs.title,
privatemsgs.created,
privatemsgs.timee,
privatemsgs.isread,
u.photo AS creatorphoto,
privatemsgs.relatedto
FROM privatemsgs
LEFT JOIN
users AS u ON(privatemsgs.useraid = u.id)
WHERE userbid='5'
AND relatedto=0 and bdel=1)
UNION ALL
(SELECT
privatemsgs.id,
privatemsgs.useradn,
privatemsgs.useraid,
privatemsgs.title,
privatemsgs.created,
privatemsgs.timee,
privatemsgs.isread,
u.photo AS creatorphoto,
rel.relatedto
FROM privatemsgs AS rel
JOIN privatemsgs ON(rel.relatedto = privatemsgs.id)
LEFT JOIN
users AS u ON(rel.useraid = u.id)
WHERE rel.userbid='5')
GROUP BY id
ORDER BY timee DESC
This query select all Privatemsgs from the tables, and acting like mail,FOR EX:
If I sent a msg to user b, and user b answered me. I want to display the msg in inbox and outbox of each user.
A comment to private msg marked as "relatedto" the id of the main msg.
The query works, but duplicate the msgs in display (same msg display many times)
I tried to do "GROUP BY id" in order to fix it but i got the error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY id ORDER BY timee DESC'
THANK YOU!!
First, as per PM 77's comment, a union instead of a union all will solve your problem of duplicates. You don't need a group by clause at all.
Second, you might have a logic error. The top have of your union query has this:
FROM privatemsgs
LEFT JOIN
users AS u ON(privatemsgs.useraid = u.id)
WHERE userbid='5'
AND relatedto=0 and bdel=1)
If any of those fields in the where clause are in the users table, your left join has become an inner join. To keep it as a left join, you have to put all the filters in the join, like this:
FROM privatemsgs
LEFT JOIN
users AS u ON privatemsgs.useraid = u.id
AND userbid='5'
AND relatedto=0 and bdel=1)
Let's say I has this sql line:
SELECT (something)
FROM user_data, user_profile_image
WHERE user_data.user_id='6' (AND user_profile_image.user_id='6')
How do I make sure that it returns informations from the user_data field if there is no containing data in user_profile_image related to that user?
You need to use JOIN
SELECT u.*,i.user_profile_image FROM user_data u LEFT JOIN `USER_PROFILE_IMAGE_TABLE` i ON i.user_id=u.user_id WHERE u.user_id='6'
I am unsure what your USER_PROFILE_IMAGE_TABLE is so you will need to change that.
SELECT something
FROM user_data
LEFT JOIN user_profile_image
ON user_profile_image.user_id = user_data.user_id
WHERE user_data.user_id = '6'
AND user_profile_image.user_id IS NULL;
You want to LEFT JOIN the detail page on the foreign key field being equal to the primary key field of the first table, and select the rows from the first table such that there IS no matching row from the second table.
In the query you gave, there's no join criteria, so it would be a Cartesian product - every row of user_data merged with every single row of user_profile_image.
Try this:
SELECT u.user_id, u.user_name, IFNULL(up.user_id, 'No Data In user_profile_image') UserId,
IFNULL(up.user_image_id, 'No Data In user_profile_image') user_image_id
FROM user_data u
LEFT JOIN user_profile_image up ON u.user_id = up.user_id
WHERE u.user_id = 6;
Check this link SQL FIDDLE DEMO
SELECT `idstudii`
FROM (`studii`)
JOIN `studii` ON `mesaje`.`idstudii`=`studii`.`id`
JOIN `users` ON `users`.`id`=`studii`.`idusers`
WHERE `studii`.`idusers` = '1'
I have this sql query which gives me the error "Not unique table/alias". This is not the case as "studii" is the only table with that name. Why does this error show up?
FROM (`studii`)
JOIN `studii`
in this care you are referring to 2 different selections of a table with the same alias (studii)
FROM `studii` AS s1
JOIN `studii` AS s2 ON s2.something2 = s1.something1