How to search for higher values - php

I have tables:
City:
id | name
1 | New York
2 | Amsterdam
3 | Paris
4 | London
Trip:
id | name
1 | aaaa // New York --> Paris --> London
2 | bbbb // London --> Paris --> Amsterdam
3 | cccc // London --> New York --> Amsterdam --> Paris
4 | dddd // Paris --> London
5 | eeee // Amsterdam --> London
TripDetails:
id | tripId | cityId| order
1 | 1 | 1 | 1
2 | 1 | 3 | 2
3 | 1 | 4 | 3
4 | 2 | 4 | 1
5 | 2 | 3 | 2
6 | 2 | 2 | 3
7 | 3 | 4 | 1
8 | 3 | 1 | 2
9 | 3 | 2 | 3
10 | 3 | 3 | 4
11 | 4 | 3 | 1
12 | 4 | 4 | 2
13 | 5 | 2 | 1
14 | 5 | 4 | 2
Now i would like find cities by city using trips.
For example if i get Amsterdam then i would like receive Paris (Trip ID = 3) and London (Trip ID = 5). NOT New York because in Trip 3 New York is before Amsterdam.
Other example:
If i get New York then i should receive all cities - Paris (Trip 1), London (Trip 1) and Amsterdam (Trip 3).
If i get Paris then i should receive only London and Amsterdam.
So i have for Paris (ID: 3):
SELECT * FROM "City"
LEFT JOIN "TripDetails" ON City.id = TripDetails.cityId
WHERE TripDetails.cityId = 3
AND ????
How can i do it?
I can change the whole structure of the database if necessary. I use PHP and MySQL.

Try something like this. This example is for New York (city id = 1) :
SQL Fiddle
Query 1:
SELECT distinct city.name
FROM tripDetails td1
INNER JOIN tripDetails td2 ON td1.tripId = td2.tripId AND
td2.order > td1.order
INNER JOIN city ON td2.cityId = city.id
INNER JOIN trip ON td2.tripId = trip.id
WHERE td1.cityID = 1
Results:
| NAME |
|-----------|
| Paris |
| London |
| Amsterdam |

Related

get unpaid staff id for this month salary sql query for codeigniter

Table A (Staffs)
id | name | salary
1 | xxxx | 300
2 | yyyy | 200
3 | zzzz | 300
Table B (Salary)
id | staffId | monthofpay | paydate
1 | 1 | jan2020 | 2020-01-01
2 | 2 | jan2020 | 2020-01-01
3 | 1 | feb2020 | 2020-02-01
4 | 2 | feb2020 | 2020-02-01
5 | 3 | feb2020 | 2020-02-01
6 | 3 | mar2020 | 2020-03-01
Table A for Staffs
Table B for Salary
i want to fetch the staff list who not pay for month of March
Try the following and here is the DEMO.
select
id
from staff st
where not exists
(
select
staffId
from salary s
where st.id = s.staffId
and monthofpay = 'mar2020'
)

MySQL pivot to make a row into a colum

I have three tables, products, customers, order
Product:
id | name |
1 | milk |
2 | bread|
3 | Pea |
Customer:
id | name | category
1 | James | retailer
2 | Paul | vendor
3 | Dave | retailer
Order:
id | product_id | customer_id | qty | price
1 | 1 | 2 | 23 | 50
2 | 2 | 2 | 4 | 30
3 | 3 | 2 | 6 | 10
4 | 2 | 1 | 9 | 30
5 | 3 | 1 | 2 | 10
6 | 1 | 3 | 6 | 50
7 | 3 | 3 | 7 | 10
When i do a query to show transactions by customers with category of vendor like
SELECT customer.name, product.name as pname, order.qty, order.price FROM customer, product, order
WHERE customer.id = order.customer_id
AND product.id = order.product_id AND customer.category = "vendor"
i will get something like:
name | pname | qty | price
Paul | milk | 23 | 50
Paul | bread | 4 | 30
Paul | pea | 6 | 10
I want this instead:
name | milk | bread | pea | total
Paul | 23 | 4 | 6 | 90
While that of retailers will look like this:
SELECT customer.name, product.name as pname, order.qty, order.price FROM
customer, product, order
WHERE customer.id = order.customer_id
AND product.id = order.product_id AND customer.category = "retailer"
I will get a table like this:
name | pname | qty | price
James | bread | 9 | 30
James | pea | 2 | 10
Dave | milk | 6 | 50
Dave | pea | 7 | 10
But i want this instead:
name | milk | bread | pea | total
James | 0 | 9 | 2 | 40
Dave | 6 | 0 | 7 | 60
Simply use conditional aggregation for pivoting columns. And be sure to use explicit joins instead of the deprecated implicit join as former has been the standard for 25 years in ANSI-92.
SELECT c.name,
SUM(CASE WHEN p.name = 'milk' THEN o.qty ELSE 0 END) as milk,
SUM(CASE WHEN p.name = 'bread' THEN o.qty ELSE 0 END) as bread,
SUM(CASE WHEN p.name = 'pea' THEN o.qty ELSE 0 END) as pea,
SUM(o.price) AS Total
FROM `customer` c
INNER JOIN `order` o
ON c.id = o.customer_id
INNER JOIN `product` p
ON p.id = o.product_id
WHERE c.category = 'vendor' -- same for retailer
GROUP BY c.name
I think that you cannot have this response structure directly from one simple select
name | milk | bread | pea | total
James | 0 | 9 | 2 | 40
Dave | 6 | 0 | 7 | 60
because your database is getting one row foreach retailers/customer order.
I know that using a server language like PHP or Java you will can handle the data and retrive like you want.

How to add field in MySQL result?

I want add one filed to result of queries in MySQL.
this tables is my database tables :
tbl_user
id_user | username | password
-----------------------------
1 | Pooria | 123456
2 | mary | 123456
3 | july | 123456
4 | Ali | 123456
5 | shahla | 123456
tbl_category
id_category | name
--------------------
1 | BMW
2 | Toyota
3 | Benz
tbl_content
id_content | message | ContentLike
--------------------------------------
1 | Best Car | 2
2 | Best Car | 3
3 | Best Car | 4
4 | Best Car | 1
5 | Best Car | 1
tbl_user_category_content
id | id_user | id_category | id_content
----------------------------------------
1 | 2 | 2 | 4
1 | 3 | 3 | 3
1 | 4 | 3 | 5
1 | 5 | 1 | 2
tbl_user_like
id_user_like | id_user | id_content
-------------------------------------
1 | 2 | 5
1 | 2 | 3
1 | 5 | 1
1 | 4 | 2
1 | 4 | 2
My Query in Mysql :
SELECT tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username
FROM tbl_user_category_content
INNER JOIN tbl_user ON
tbl_user_category_content.id_user = tbl_user.id_user
INNER JOIN tbl_cat ON
tbl_user_category_content.id_category = tbl_cat.id_category
INNER JOIN tbl_post ON
tbl_user_category_content.id_content = tbl_content.id_content
and Result
id_content | message | ContentLike | username
----------------------------------------------
1 | Best Car | 2 | Pooria
2 | Best Car | 4 | mary
3 | Best Car | 3 | july
4 | Best Car | 5 | Ali
5 | Best Car | 4 | shahla
I expect Result :
I want insert a id_user and view Result:
example : insert id_user : 2 (mary)
id_content | message | ContentLike | username | Like_User
-------------------------------------------------------------------
1 | hello world1 | 4 | Pooria |
2 | hello world2 | 2 | mary |
3 | hello world3 | 2 | july | Yes
4 | hello world4 | 2 | Ali |
5 | hello world5 | 2 | shahla | Yes
add result Like_User
important:I want all entries in the database with the person who likes to show a distinct field. thanks you for help me <3
Add this in the select columns :
case when tbl_user.id_user=tbl_user_like.id_user then 'Yes' else 'No' end as Like_User
So it would be something like this
SELECT tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username, case when tbl_user.id_user=tbl_user_like.id_user then 'Yes' else 'No' end as Like_User
FROM tbl_user_category_content
INNER JOIN tbl_user ON
tbl_user_category_content.id_user = tbl_user.id_user
INNER JOIN tbl_cat ON
tbl_user_category_content.id_category = tbl_cat.id_category
INNER JOIN tbl_post ON
tbl_user_category_content.id_content = tbl_content.id_content
I have not test it. Try it out and maybe you should change the column names in the case
Try This Query
SELECT distinct tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username,tbl_user_category_content.category
FROM tbl_user_category_content
INNER JOIN tbl_user,tbl_cat,tbl_post ON
tbl_user_category_content.id_user = tbl_user.id_user
tbl_user_category_content.id_category = tbl_cat.id_category
tbl_user_category_content.id_content = tbl_content.id_content
WHERE tbl_user.id_user='$user_id'

mysql LEF JOIN with LIMIT

I have 2 tables:
games:
g_id | country | team_1 | team_2
--------------------------------
1 | England | Bayern | Chelsea
2 | England | Bayern | Liverp
3 | England | Bayern | Ajax
statistic:
s_id | s_time | s_name | g_id
-----------------------------
1 | 4 | Alen A. | 1
2 | 7 | Dagn S. | 1
3 | 11 | Eden D. | 1
4 | 22 | Aren A. | 1
5 | 8 | Falen B. | 2
6 | 66 | Poker G. | 2
7 | 76 | Nuker S. | 2
8 | 87 | Eben Y. | 2
9 | 18 | Falen B. | 3
10 | 19 | Aalen F. | 3
11 | 33 | Gased G. | 3
12 | 44 | Halen B. | 3
And i'm trying to get data from 2 tables with left join where limit
here a query:
SELECT *
FROM games
LEFT JOIN statistic
ON games.g_id = statistic.g_id
WHERE games.team1 = 'Bayern'
LIMIT 2
result is:
g_id | country | team_1 | team_2 | s_id | s_time | s_name | g_id
------------------------------------------------------------------
1 | England | Bayern | Chelsea| 1 | 4 | Alen A. | 1
1 | England | Bayern | Chelsea| 2 | 7 | Dags S. | 1
i need all data from statistics with limit 2 from table "games"! here example what i need:
g_id | country | team_1 | team_2 | s_id | s_time | s_name | g_id
------------------------------------------------------------------
1 | England | Bayern | Chelsea| 1 | 4 | Alen A. | 1
1 | England | Bayern | Chelsea| 2 | 7 | Dags S. | 1
1 | England | Bayern | Chelsea| 3 | 11 | Eden D. | 1
1 | England | Bayern | Chelsea| 4 | 22 | Aren A. | 1
2 | England | Bayern | Liverp | 5 | 8 | Falen B. | 2
2 | England | Bayern | Liverp | 6 | 66 | Dags S. | 2
2 | England | Bayern | Liverp | 7 | 76 | Alen A. | 2
2 | England | Bayern | Liverp | 8 | 87 | Dags S. | 2
What query i need?
You can try this:
SELECT *
FROM (SELECT * FROM games WHERE team1 = 'Bayern' ORDER BY g_id LIMIT 2) AS g
LEFT JOIN statistic AS s
ON g.g_id = s.g_id

MySQL join - ordering results via another table PHP

I have 2 MySQL tables, one of which has a numeric column to orgainise the order I need the items to be displayed:
item_names
menu_id | dish_id | section_id | item_name
--------------------------------------------------
1 | 23 | 2 | Pie
1 | 24 | 2 | Fish
1 | 25 | 3 | Apples
1 | 26 | 2 | Onions
1 | 27 | 2 | Chips
link_extras
extra_id | dish_id | sort
-----------------------------
1 | 23 | 2
2 | 23 | 2
3 | 23 | 2
1 | 24 | 0
5 | 24 | 0
6 | 26 | 3
12 | 26 | 3
1 | 27 | 1
1 | 25 | 0
Basically what I am trying to do is extract each dish with a certain menu_id and section_id from the table item_names and order the output in respect to the sort column in the link_extras table.
so far:
$query="SELECT a.item_name, a.dish_id, b.sort
FROM item_names AS a, link_extras AS b
WHERE a.menu_id='1'
AND a.section_id='2'
AND b.dish_id=a.dish_id
GROUP BY b.dish_id
ORDER BY b.sort";
I am quite new to databases so would appreciate any help. The result I am after is
Fish
Chips
Pie
Onions
Unfortunately just can't get the order correct.
You need to use a simple JOIN
SELECT a.item_name, a.dish_id, b.sort
FROM item_names AS a
JOIN link_extras AS b
ON a.dish_id = b.dish_id
WHERE menu_id = 1
AND section_id = 2
GROUP BY b.dish_id
ORDER BY b.sort
Output:
| ITEM_NAME | DISH_ID | SORT |
------------------------------
| Fish | 24 | 0 |
| Chips | 27 | 1 |
| Pie | 23 | 2 |
| Onions | 26 | 3 |
See this SQLFiddle
SELECT
in.item_name
FROM item_names AS in
LEFT JOIN link_extras AS le
ON le.dish_id = in.dish_id
WHERE in.menu_id = 1
AND in.section_id = 2
ORDER BY le.sort
Demo Here
Output
| ITEM_NAME |
-------------
| Fish |
| Chips |
| Pie |
| Onions |

Categories