drupal|December 30, 2021|1 min read

Drupal Mysql Query to Fetch User Field Details and its Alias

TL;DR

Join users_field_data, user field tables, and path_alias to fetch user details including custom fields and URL aliases in a single SQL query.

Drupal Mysql Query to Fetch User Field Details and its Alias

Introduction

In this posr, we will see how to prepare mysql query to fetch user details, its fields, and its alias.

Mysql DB Query

select data.name, data.mail, from_unixtime(data.created), 
from_unixtime(data.changed), fname.field_full_name_value 
from users user  
left join user__field_full_name fname on user.uid = fname.entity_id  
left join users_field_data data on user.uid = data.uid  
left join path_alias alias on alias.path like CONCAT('/user/', user.uid) 
where data.status=1 limit 0,1000;

In above query,

  • I have converted integer timestamp for fields (created, changed) to date-time format by using from_unixtime function
  • Here, I have a field named full_name.
  • The alias scheme is /users/<username>

To get the output data as JSON

select JSON_OBJECT('name', data.name, 'email', data.mail, 
'created', from_unixtime(data.created), 'changed', from_unixtime(data.changed), 
'full_name', fname.field_full_name_value)  
from users user  
left join user__field_full_name fname on user.uid = fname.entity_id  
left join users_field_data data on user.uid = data.uid  
left join path_alias alias on alias.path like CONCAT('/user/', user.uid) 
where data.status=1 limit 0,1000;

Also see Drupal Code to Fetch Active users

Related Posts

Drupal DB Query Code to Fetch Active Users and Accessed Website Within last One Year

Drupal DB Query Code to Fetch Active Users and Accessed Website Within last One Year

Introduction Here, we will see the drupal code to fetch all the active users…

How to Fix Drupal Mysql error - Communication link failure: 1153 Got a packet bigger than 'max_allowed_packet' bytes

How to Fix Drupal Mysql error - Communication link failure: 1153 Got a packet bigger than 'max_allowed_packet' bytes

Introduction While this topic may applicable to all mysql/mariadb users who…

Drupal Code to run Database Query to Fetch Active User Details, Count, Pagination

Drupal Code to run Database Query to Fetch Active User Details, Count, Pagination

Introduction This post shows code to run database query to fetch active user…

Drupal 8 - How to Theme Form and its Fields with reordering fields

Drupal 8 - How to Theme Form and its Fields with reordering fields

Introduction In this post, we will see how to theme form and its fields…

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Introduction I needed a report page, where I wanted to have some information…

Drupal 7 - Code for Exporting all your content nodes in json files

Drupal 7 - Code for Exporting all your content nodes in json files

Introduction When I migrated all of my drupal-7 website to drupal-8, I wrote…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…