I have a users table and a categories table which already contain data,
I also have a third table which called user_category which has three columns(id,user_id,category_id).
When the user first register he must choose a category or more than one from a dropdown. I want to insert the id of the user to user_id columns and the id of each category to the category_id so if the user choose more than one category he will have more than one record in the user_category table.
Use This
SELECT user_id FROM users
ORDER BY user_id DESC
LIMIT 1
This will give you the last user_id which has inserted Now by using this user id you can insert the category_id by for each loop or for loop
Related
I have 2 tables
From table 1(student): I want id, name, and roll_no
From table 2(score): I want SUM(total), SUM(score) with some condition
And I want all student same class data from student table combine with score table to get each student SUM(total) and SUM(score) of some condition in student table.
In score table I have student_id name column and in student
I have id column which can be linked to each other if possible...
How can I achieve this??
I want this all-in-1 query to get the data in ascending order by score column...
I am making a skill sharing website for a project.Tables in database are as follows:
users(id,name,email,password)
skills(id,skillname)
haveskills(id,user_id,skill_id)
{user_id references id in users and skill_id references id in skills}
wantskills(id,user_id,skill_id)
{user_id references id in users and skill_id references id in skills}
friends(id,user_id1,user_id2)
{say user with id 1 and 3 are friends then I have two rows first:user_id1=1,user_id2=3;second:user_id1=3,user_id2=1}
When a user is logged in I have these infos:His
id,haveskill_id(hid),wantskill_id(wid).
I want sql query for all the users id whose
haveskill_id=wid,wantskill_id=wid and are NOT friends.
Edit:My query which fulfills first two conditions but I am having problem with third condition:
SELECT A.id FROM users A JOIN haveskills B
ON (A.id=B.user_id And B.skill_id='$wantskill_id')
JOIN wantskills C
ON (A.id=C.user_id AND C.skill_id='$haveskill_id');
Hey I have the following MYSQL DB structure for 3 tables with many to many relation. Many users can have many cars and cars can be for many users as showing below:
Users
ID | Name
---------
100|John
101|Smith
Cars
ID | Name
---------
50|BMW
60|Audi
Users_cars
ID | UID | CID
---------
1| 100 |50
2| 100 |60
3| 101 |60
I have a page users_cars.php this page have two drop down lists
list of all users
list of all cars
In this page you can select a user from user's list and select a car from car's list then click add to insert into users_cars table.
What am trying to do is to exclude from user's drop down list all the users that have been linked with all the available cars from cars table.
In the example above user's drop down list will just have "Smith" because "John" linked with all cars available (BMW,AUDI), if "Smith" also has the BMW he will be excluded from the list. I need a select query for this condition and i don't want to use any nest select query to count user records inside users_cars table
If I understand what you are after you need to use GROUP BY in your query. So to select all users:
SELECT ID, UID FROM Users_cars GROUP BY UID
and for all cars:
SELECT ID, CID FROM Users_cars GROUP BY CID
That will group results that are the same, so you only get one instance of each user, or one instance of each car.
I hope I understood your question right.
I think you can so this using some programming -
With PHP/mysql -
Get count of all distinct car ID's
Get count of cars for each user. (making sure this lists only unique car ID's)
Loop through all users and in each loop compare the above two and exclude the user where this condition matches.
SELECT *
FROM users
WEHRE id NOT IN (SELECT uid
FROM (SELECT uid, COUNT(cid), COUNT(*)
FORM cars
LEFT OUTER JOIN users_cars ON cars.id = users_cars.cid
GROUP BY uid
HAVING COUNT(cid) = COUNT(*)
Basically, what you want to do is that (if I understood your problem) :
SELECT UID FROM Users_cars WHERE CID NOT IN (SELECT ID FROM Cars);
But carefull, this is a greedy request (depends on the size of the tables of course) and you should better put a flag on the user table and update then when your user uses the last available car (or with a batch) so you don't run the request too often !
Hi i am not sure how to put this in a brief sentences, but i have DB table like the following
User Table
user_id
username
and so on...
Item
item_id
item_name
Item_Equipped
equipped_id head (FK to item_id)
hand (FK to item_id)
user_id (FK to user_id IN User Table)
I would like to generate a query that will display like the following format
user_id | head | head_item_name | hand | hand_item_name | ...
So far i only able to do this:
SELECT user.user_id, user.username,
equipments.head, equipments.head_acc,
equipments.hand,
equipments.acc, equipments.body
FROM gw_member_equipped AS equipments
LEFT JOIN gw_member AS user ON user.memberid = equipments.member_id
Which (i have to be brutally honest) doesn't do anything much.
I tried to perform INNER JOIN between item and item_equipped however i am unable to get individual name for each item (based on its item ID)
you need to join ITEM table two times with ITEM_EQUIPPED table.
you can use below query for your desired output column shown in question..
SELECT USER.User_Id,
Item_Equipped.Head,
Item_Heads.Item_Id Head_Item_Id, -- if you want you can remove this column
Item_Heads.Item_Name Head_Item_Name,
Item_Equipped.Hand,
Item_Hands.Item_Id Hand_Item_Id, -- and this column also as these columns are same as their previous select columns
Item_Hands.Item_Name Hand_Item_Name
FROM USER, Item Item_Heads, Item Item_Hands, Item_Equipped
WHERE USER.User_Id = Item_Equipped.User_Id
AND Item_Heads.Item_Id = Item_Equipped.Head
AND Item_Hands.Item_Id = Item_Equipped.Hand
I am new to cakephp. I have table customers with this
id, product_id, created, modified
and my product table has this
id, label, cost
I want to select all the records from customer table which has the product_id in product table (join on id may be)?
How can I do that
You should write a hasMany association in your model for the table you would like to query, then do your $this->Model->find('all'...) query and make sure in your options you specify recursive=>1 so that it will get your associated data. http://book.cakephp.org/view/1043/hasMany