I have two tables: products and products_actions.
When the user clicks away a product, I want this action to be stored in products_actions. The field has_removed will be 1 then.
I would like to present to the user only those products that he has not clicked away.
Currently I have the following entries in my tables:
Table "products":
id: 1
name: Product 1
id: 2
name: Product 2
id: 3
name: Product 3
Table "products_actions":
id: 1
id_product: 2
has_removed: 1
If the user has not removed a product from his page so far, there will be no corresponding entry in the products_actions table.
So my query is:
$qb->select('p')
->leftJoin(
'ProductsActions',
'pa',
'WITH',
'pa.idProduct = p.id'
)
->where('pa.hasRemoved != 1');
How do I achieve that my query aboves delivers "Product 1" and "Product 3" as entries?
I am not entirely clear what you need. I guess you are talking probably like this :
select * from products where id not in (select id_product from products_actions where hasRemoved = 1)
Related
I have Five tables. They are: category ,Product , product_categories, Order and order products
Category Table
id
Category Title
1
Category One
2
Category Two
Product Table
id
product Title
1
product One
2
product Two
Product Categories Table
id
product_id
category_id
1
1
2
2
2
1
Order Table
id
Order Title
1
Order One
2
Order Two
Order Products table
id
product_id
order_id
1
1
2
2
2
1
So now, I want Category order count through product's product_categories table and Order's order_products table.
It would be very easy if
product table have category_id and order table have product_id so that we can easily fetch order count through hasManyThrough relationship.
This is my code that i have tried but it only counts product item.
$orders['topCategories'] = ProductCategory::with('childs.childs')->withCount(['products'=>function ($query) {
$query->whereHas('orders', function ($query){
$query->where('status', true);
});
}]);
So I have 2 tables that I'm using. A product table that will fill out some product cards using PHP. What I want to do if make an sql statement to only pull information from the product table if the user has failed a red/green or blue/yellow colorblind test that we setup. So if the user has failed a red/green colorblind test, I want to only pull the product information from the products that have a lenstype of red. And the designation of if they have failed the test is marked as a 1.
Looking for an SQL statement to make this happen.
Userreg
id
bluefail
redfail
1
1
0
2
0
1
3
1
0
Product
prodtitle
lenstype
price
1
blue
10
2
red
10
3
red
10
You can use join like this:
select u.*, p.*
from userreg u join
product p
on (p.lenstype = 'blue' and u.bluefail = 1) or
(p.lenstype = 'red' and u.redfail = 1);
Note that if a user fails both, then they will get products for both types.
I have product table with product name and foreign key column named as subcategory I want to get product based on subcategory id and product name using or condition in codeigniter query method I wrote query for that but it is retrieving two record but i need only one record
for example we have table:
productid product subcategory
1 A 1
2 A 3
3 B 3
$var="some value from form(1 or 3)";
case 1: if product='A' and subcategory($var)=1 or subcategory ($var)=3 then it should get record with productid=1
case2 :if product='A' and subcategory($var)=2 or subcategory ($var)=3 then it should get record for productid=2 because we are using or condition it should check 1 or 3
the query which I have used it is retrieving two records but I need only one record if subcategory =1 or 3
Query:
$this->db->where('product', 'A');
$where = '(subcategory="1" or subcategory = "3")';
$this->db->where($where);
$this->db->get('product');
$query = $gsm_pricing = $query->row();
This query solved my issue
$this->db->query(select * from product where product='A'
and subcategory='1'
union all
select * from product where product='A' and subcategory='3'
and not exists(select 1 from product where subcategory='1' and prodduct='A'));
Try using " group by productid " in your Query
Hello friends I have 2 Mysql tables with 1:N relationship between category and category_Dates
Category:
ID Category Frequency
1 Cat A Half-yearly
2 Cat B Quarterly
category_Dates:
ID CatID Date
1 1 01-Jan-15
2 1 01-Jul-15
3 2 01-Jan-15
4 2 01-Apr-15
5 2 01-Jul-15
6 2 01-Oct-15
based on the category frequency I am entering number of records automatically in category_date. Eg
When category frequency = quarterly, I am entering 4 records in category_date with ID of that category. And dates will be entered later.
I am little confused if in case on wants to edit the frequency from halfyearly to yearly. How to change number of records. Please help with your valuable suggestions. I am using laravel 4 framework with mysql
best way would be with 3rd table joining Dates and Categories. See little carefully ,you can see its actually Many to Many relationship (N to N) as 1 category can have multiple dates. and one date may be part of multiple categories, like say 01-Jan-15 is part of Category 1 and 2 as well.
So use
category table
id Category Frequency
1 Cat A Half-yearly
2 Cat B Quarterly
date table
id Date
1 01-Jan-15
2 01-Apr-15
3 01-Jul-15
4 01-Oct-15
categories_dates table
ID CatID Date_id
1 1 1
2 1 3
3 2 1
4 2 2
5 2 3
6 2 4
If you change the frequency in Category table, retrieve the update category_id,
delete all from category_dates where CatId=category_id then insert the new entries in category_Dates.
Hope this help.
I assume your models are Category and CategoryDates.
let's update category id 1 from Half-yearlyto to Quarterly
$query = Category::find(1);
$query -> Frequency = 'Quarterly';
$query -> save();
return $query -> id;
in the CategoryDates model you would delete the catID = 1 and insert new data
$catID = 1;
$query = CategoryModel::where('CatId',$catId) -> delete();
$data = ['CatId' => $catID,'date' => 01-Jan-15, ....];
CategoryModel::create($data);
of course assuming that you would return the newly updated category id to your controller and call a funtionn to do the update in your CategoryModel.
Hope this help.
There are 2 MySQL tables: 'posts' and 'category'. Unfortunately there is no taxonomy table.
Categories:
id: integer
name: string
Posts:
id: integer
name: string
body: text
tag: string
category: string
So, in category there is:
id name
1 Books
2 Animals
And Post:
id name body tag category
1 My Post Hi post my-post 1
2 Post 2 2nd Post post-2 1;2
This is te SQL query:
SELECT * FROM posts WHERE category = '1'
Only returns post id 1
SELECT * FROM posts WHERE category = '2'
Returns nothing
How can I get both posts with just one SQL query?
Personally, I would steer well clear of that structure and create a new table PostCategoryto house your associated categories for each post so:
postID | categoryID
1 | 1
2 | 1
2 | 2
Then use a distinct select and an inner join in your sql:
select distinct post.* from post
inner join postcategory on post.id = postcategory.postID
where postcategory.categoryID = 2;
As #McAdam331 quite rightly said, using a string to store lookup values you're going to be querying is B.A.D for performance and in general for db design