This question already has answers here:
OR WHERE and AND in mySQL Query
(2 answers)
Closed 3 years ago.
I am new to this forum. I hope I will be able to explain my question to everyone.
I want to call the data from columns (WareParkOut or/and EndKm) with specific user. But the query I am using in my session is showing the data from other user id's too
$sql = "SELECT *
from tblleaves
where empid=:eid
AND WareParkIn = ''
or EndKm = ''
order by PostingDate desc";
I am finding it hard to use AND or operators here which I believe is the problem.
AS a result, I want to call everything from table tblleaves in every customer login if WareParkIn is empty or EndKM is empty or both are empty. But it is showing all customers empty data in every login. I want it login specific.
e.g User ID 1's login is showing other users WareParkIn or EndKM too
$sql = "SELECT * from tblleaves
where empid=:eid
AND (WareParkIn = '' or EndKm = '')
order by PostingDate desc";
Related
This question already has answers here:
SELECT * EXCEPT
(15 answers)
Closed 1 year ago.
Suppose I have a table that contains "200" columns, how do I select all colons except a few, and the way I know is to select it manually:
"SELECT id, user, name, email, city... FROM table WHERE id = 1";
However my wish would be something like:
"SELECT * (exeto essas tabelas) FROM table WHERE id = 1";
The only way you can achieve that in MySQL is to use hidden columns (https://dev.mysql.com/doc/refman/8.0/en/invisible-columns.html).
This question already has answers here:
How to SELECT DISTINCT *
(2 answers)
Closed 3 years ago.
Currently when a user would open this web page, all of the products in the database are listed on the page. Some products show up in multiple categories which means that they are listed multiple times, how can I limit them to showing up once?
Here you can see some examples on how you can choose random records from database. However, keep that in mind, that described method is not the most efficient one.
http://www.mysqltutorial.org/select-random-records-database-table.aspx
In your example, it would just need to be:
$query = "SELECT * FROM product WHERE product_status = '1' ORDER BY RAND() LIMIT 12";
This question already has answers here:
How do I check if a column is empty or null in MySQL?
(21 answers)
Closed 3 years ago.
I am creating a table where I want to show the data where one column is empty.
I tried everything but nothing seems to work
$sql = "SELECT * from tblleaves where empid=:eid and EndKm is NULL order by PostingDate desc";
In other words, I want to fetch data where endkm column is empty or has spaces only.
If I remove EndKm is NULL everything works fine for me.
Try SELECT * from tblleaves where empid=:eid and (EndKm IS NULL or TRIM(EndKm) = '') order by PostingDate desc
This question already has answers here:
Ordering by specific field value first
(8 answers)
Closed 3 years ago.
I have a table in my database called user_data.
I want to select all rows of data from this table, where the user's advert_status is 'Active'.
However, there is also a column called advert_type in this table. The enum values of this are:
Deluxe, Premium and Basic.
I want to execute a MYSQLI query and list all users in the table where their advert_status is active. But i want to list deluxe users, then premium users and basic users - in that order.
Desired Result:
Deluxe User 1
Deluxe User 2
Deluxe User 3
Premium User 1
Premium User 2
Basic User 1
Basic User 2
Here's what i've tried.
$variable = $_GET['r'];
if($variable == '0') {
$query = $conn->query("SELECT * FROM user_data WHERE advert_status = 'Active' ORDER BY advert_type");
This doesn't do the job. Please can someone show me how to achieve my desired result? Thanks in advance.
You can use order by case when to fulfill your requirement:
SELECT * FROM user_data
WHERE advert_status = 'Active'
ORDER BY
Case when advert_type = "Deluxe" then 1
when advert_type = "Premium" then 2
else 3 end asc
This question already has answers here:
TOP and ORDER BY sql error
(3 answers)
Closed 6 years ago.
I would like to display only the latest one of those rows which match to my conditions. It will be a conversation listing page where I would like to display only the latest message from the user or his/her partner, and then display the next conversation's latest message between the user and another partner, and so on...
I have this database:
And this code to fetch data:
$db = new PDO("mysql:host=localhost; dbname=dbname", 'root', '');
$stmt4 = $db->prepare("
SELECT *
FROM messages
WHERE messages.from_id=7
OR messages.to_id=7
ORDER BY msg_id DESC
");
$stmt4->execute();
Is it possible somehow?
You only need add LIMIT 1 to your query.
SELECT *
FROM messages
WHERE messages.from_id=7
OR messages.to_id=7
ORDER BY msg_id DESC
LIMIT 1
You can just do
select *
FROM messages
WHERE messages.from_id=7
OR messages.to_id=7
ORDER BY timesent DESC
LIMIT 1
That should work per your requirements, it will only pull in the top record for what is matching your where clause and it will be ordered by the latest time sent.