I have 3 tables:
products (->belongsToMany('Series'))
series (->belongsToMany('Product'))
product_series
If I want to take products with series = 3:
Series::find(3)->products;
Produced sql query:
SELECT *
FROM `products`
INNER JOIN `product_series`
ON `products`.`id` = `product_series`.`product_id`
WHERE `product_series`.`series_id` = '3'
The question is how can I take products where series_id != 3 with Eloquent?
The sql query is smth like:
SELECT *
FROM `products`
INNER JOIN `product_series`
ON `products`.`id` = `product_series`.`product_id`
WHERE `product_series`.`series_id` != '3'
You may try this:
$series = Series::where('id', '!=', 3)->with('products')->get();
Related
I have 3 tables Order, CheckoutStatus, and Statuses. There is a foreign key in Order and CheckoutStatus that references the Statuses table.
I need to join CheckoutStatus to Order linked by the PO column and I need to join Statuses to Order and to CheckoutStatus.
Here is the data in the tables
Order table
`PO` = 123456
foreign key `Statuses_id` = 2
CheckoutStatus
`PO` = 123456
foreign key `Statuses_id` = 0
Statuses
`id` 0 = Complete
`id` 2 = Completed
How do I write my SQL statement so that I can have a result like this.
Order
123456
Completed
CheckoutStatus
123456
Complete
This SQL statement I'm using does not display anything unless I remove one of the JOIN Statuses section of the statement.
SELECT * FROM `Order` JOIN `Statuses` ON Statuses.id = Order.Statuses_id JOIN `CheckoutStatus` ON Order.PO = CheckoutStatus.PO JOIN `Statuses` ON Statuses.id = CheckoutStatus.Statuses_id
You have two JOINs on table Statuses. You need to use table aliases to distinguish the two relationships :
SELECT
`Order`.PO,
s1.Status,
s2.Status
FROM
`Order`
JOIN `Statuses` s1 ON s1.id = Order.Statuses_id
JOIN `CheckoutStatus` ON Order.PO = CheckoutStatus.PO
JOIN `Statuses` s2 ON s2.id = CheckoutStatus.Statuses_id
this can get a little bit confusing you can split the query and on the basis of its fetched result, you can fetch further more
$sql4 = "SELECT * FROM `Order` JOIN `Statuses` ON Statuses.id =
Order.Statuses_id JOIN `CheckoutStatus` ON somthing"
$result4 = mysqli_query($conn, $sql4);
if (mysqli_num_rows($result4) > 0)
{
while($row3 = mysqli_fetch_assoc($result4))
{
$sql5='SELECT * FROM //the result of before joining other two '
}
}
something like this
I am trying to to do a cart where the products have customizations... To avoid lot of queries in the data base I have tried to save the results in an array after the query. The query looks like this:
$sql = 'SELECT *
FROM cc_restaurants_menu
WHERE menu_id
IN ("7","50","50")
ORDER BY FIELD (menu_id,"7","50","50")';
$result = $conn->query($sql);
if($result->rowCount() > 0) {
while($row = $result->fetch()) {
$names[] = $row['menu_product'] .'<br>';
}
}
But the problem with this is that the query avoid the repeated ids. (I need repeated ids because the people can add the product more than one time).
Is there anyway to get the repeated ids more than one time?
The only idea I have is do something like (if the ids are in an array) :
foreach($ids as $d) {
//query here the database based in each id
}
thanks in advance
Think you might need one query for each menu_id, and use UNION ALL to join the results together.
Something like this:-
SELECT *
FROM cc_restaurants_menu
WHERE menu_id = "7"
UNION ALL
SELECT *
FROM cc_restaurants_menu
WHERE menu_id = "50"
UNION ALL
SELECT *
FROM cc_restaurants_menu
WHERE menu_id = "50"
ORDER BY FIELD (menu_id,"7","50","50")
Or build up the required menu ids in a sub query which you then join against the main table.
SELECT cc_restaurants_menu.*
FROM cc_restaurants_menu
INNER JOIN
(
SELECT 7 AS a_menu_id UNION ALL SELECT 50 UNION ALL SELECT 50
) sub0
ON cc_restaurants_menu.menu_id = sub0.a_menu_id
ORDER BY FIELD (cc_restaurants_menu.menu_id,"7","50","50")
Generating the first query with implode would be done something like this:-
$sql = "SELECT * FROM cc_restaurants_menu WHERE menu_id = '".implode("' UNION ALL SELECT * FROM cc_restaurants_menu WHERE menu_id = '", id_array)."' ORDER BY FIELD (menu_id,'".implode("','", id_array)."')";
and generating the 2nd query with implode would be done something like this:-
$sql = "SELECT cc_restaurants_menu.* FROM cc_restaurants_menu INNER JOIN (SELECT ".implode(" AS a_menu_id UNION ALL SELECT ", id_array) AS a_menu_id ) sub0 ON cc_restaurants_menu.menu_id = sub0.a_menu_id ORDER BY FIELD (cc_restaurants_menu.menu_id,".implode(",", id_array).")";
I have table where I store data on basis of date.
Now I need to check that: is there any difference between data of rows with two different dates.
In a simple way you can say that I have two queries which select data from same table now I have to compare each row and column value. for example my two query are -
SELECT * FROM `national` WHERE `upload_date` = '2015-08-04' // return 106 rows
and
SELECT * FROM `national` WHERE `upload_date` = '2015-08-01' // return 106 rows
I have tried to compare this with below query but the result not seem to be correct to me, I am not satisfy with this.
Select U.* from
(SELECT t1.* FROM `national` t1 where upload_date = '2015-08-01'
union all
SELECT t2.* FROM `national` t2 WHERE `upload_date` = '2015-08-04' ) U
GROUP BY emp_id, uqi_id
HAVING COUNT(*) = 1
Can Any one please provide me correct query ?? thank you
Try this
(
SELECT t1.*
FROM
`national` t1, `national` t2
where
t1.upload_date = '2015-08-01' and t2.upload_date='2015-08-04' and
-- put your columns here that you want to compare for same DATA
-- like t1.name=t2.name and etc...
)
You can try with something like that
SELECT * FROM (SELECT * FROM `national` WHERE upload_date = '2015-08-01') a
INNER JOIN (SELECT * FROM `national` WHERE upload_date = '2015-08-04') b
ON a.emp_id = b.emp_id AND a.uqi_id = b.uqi_id
ORDER BY uqi_id
i'm trying to convert this SQl query to QueryBuilder but i can't do it.
SELECT a.id, a.category_name, cat.Count
FROM `categories` a
LEFT OUTER JOIN (
SELECT `categories`.`category_parent` , COUNT(*) AS Count
FROM `categories`
GROUP BY category_parent
)
cat ON a.id = cat.category_parent
WHERE a.category_parent = 1
for example:
DB::table('users')
->join('contacts',DB::raw("a.id = cat.category_parent"),function($query){
$query->select(DB::raw("`categories`.`category_parent` , COUNT( * ) AS Count"))
->from('contacts')
->groupBy('category_parent')
->where(DB::raw("a.category_parent = 1"))
->get();
how to fix this method in laravel. thanks
Try getting the PDO instance to execute your query directly:
$PDO=DB::connection('mysql')->getPdo();
$stmt=$PDO->prepare("
SELECT a.id, a.category_name, cat.Count
FROM `categories` a
LEFT OUTER JOIN (
SELECT `categories`.`category_parent` , COUNT(*) AS Count
FROM `categories`
GROUP BY category_parent
)
cat ON a.id = cat.category_parent
WHERE a.category_parent = :category_parent
");
$stmt->bindParam(':category_parent', 1);
$stmt->execute();
$result = $stmt->fetchAll();
I have a query like that in one of my projects running on laravel 4.
Good Luck!
I have this sql statement generated from codeigniter
SELECT * FROM (`products`) LEFT JOIN `products_attributes` ON `products_attributes`.`product_id` = `products`.`id` WHERE `products`.`category` = '1' AND `products`.`sub_category` = '1' AND `products_attributes`.`attribute_id` = '3' AND `products_attributes`.`attribute_id` = '4' AND `products_attributes`.`attribute_id` = '5' GROUP BY `products`.`id`
and I have these tables
products :
products_attributes :
I need to get the products with the all attributes only, so I will get the product id = 1 if the attributes are 3,4,5 and if the attributes are 3,4,5,6 I will not get product id = 1
Your sql query is totally wrong. What you did is:
Give me the record the attribute_id = 3 AND attribute_id = 4 AND attribute_id = 5 AND attribute_id = 6. One row can not be equal to 3 and 4 at the same time.
What you need can be achieved in many different ways. For example:
SELECT
products.id
FROM products
JOIN products_attributes ON products_attributes.product_id = products.id
WHERE
products.category = '1'
AND `products_attributes`.`attribute_id` IN (3,4,5,6)
GROUP BY
products.id
HAVING COUNT(DISTINCT `products_attributes`.`attribute_id`) = 4
If you specify just IN ... then it will take all the records who has at least 1 such attribute_id
Try this:
SELECT * FROM (`products`)
LEFT JOIN `products_attributes`
ON `products_attributes`.`product_id` = `products`.`id`
WHERE `products`.`category` = 1
AND `products`.`sub_category` = 1
AND `products_attributes`.`attribute_id` IN (3, 4, 5)
GROUP BY `products`.`id`